@@ -180,7 +180,7 @@ public struct BigInt: Hashable,
180
180
self . init ( words: y. uwords, negative: value < 0.0 )
181
181
}
182
182
}
183
-
183
+
184
184
/// Creates a `BigInt` from a sequence of digits for a given base. The first digit in the
185
185
/// array of digits is the least significant one. `negative` is used to indicate negative
186
186
/// `BigInt` numbers.
@@ -254,6 +254,26 @@ public struct BigInt: Hashable,
254
254
self . init ( digits: temp, negative: negative, base: base)
255
255
}
256
256
257
+ /// Initializes a `BigInt` randomly with a given number of bits
258
+ public init < R: RandomNumberGenerator > ( randomWithMaxBits bitWidth: Int ,
259
+ using generator: inout R ) {
260
+ var words = ContiguousArray < UInt32 > ( )
261
+ let capacity = ( bitWidth + UInt32. bitWidth - 1 ) / UInt32. bitWidth
262
+ if capacity > 2 {
263
+ words. reserveCapacity ( capacity)
264
+ }
265
+ var bits = bitWidth
266
+ while bits >= UInt32 . bitWidth {
267
+ words. append ( generator. next ( ) )
268
+ bits -= UInt32 . bitWidth
269
+ }
270
+ if bits > 0 {
271
+ let mask : UInt32 = ( 1 << bits) - 1
272
+ words. append ( ( generator. next ( ) as UInt32 ) & mask)
273
+ }
274
+ self . init ( words: words, negative: false )
275
+ }
276
+
257
277
/// Converts the `BigInt` object into a string using the given base. `BigInt.DEC` is
258
278
/// used as the default base.
259
279
public func toString( base: Base = BigInt . decBase) -> String {
@@ -930,6 +950,39 @@ public struct BigInt: Hashable,
930
950
}
931
951
return BigInt . fromTwoComplement ( & words)
932
952
}
953
+
954
+ /// Returns a random `BigInt` with up to `bitWidth` bits using the random number
955
+ /// generator `generator`.
956
+ public static func random< R: RandomNumberGenerator > ( withMaxBits bitWidth: Int ,
957
+ using generator: inout R ) -> BigInt {
958
+ return BigInt ( randomWithMaxBits: bitWidth, using: & generator)
959
+ }
960
+
961
+ /// Returns a random `BigInt` with up to `bitWidth` bits using the system random number
962
+ /// generator.
963
+ public static func random( withMaxBits bitWidth: Int ) -> BigInt {
964
+ var generator = SystemRandomNumberGenerator ( )
965
+ return BigInt ( randomWithMaxBits: bitWidth, using: & generator)
966
+ }
967
+
968
+ /// Returns a random `BigInt` below the given upper bound `bound` using the random number
969
+ /// generator `generator`.
970
+ public static func random< R: RandomNumberGenerator > ( below bound: BigInt ,
971
+ using generator: inout R ) -> BigInt {
972
+ let bitWidth = bound. bitSize
973
+ var res = BigInt ( randomWithMaxBits: bitWidth, using: & generator)
974
+ while res >= bound {
975
+ res = BigInt ( randomWithMaxBits: bitWidth, using: & generator)
976
+ }
977
+ return res
978
+ }
979
+
980
+ /// Returns a random `BigInt` below the given upper bound `bound` using the system random
981
+ /// number generator.
982
+ public static func random( below bound: BigInt ) -> BigInt {
983
+ var generator = SystemRandomNumberGenerator ( )
984
+ return BigInt . random ( below: bound, using: & generator)
985
+ }
933
986
}
934
987
935
988
0 commit comments