I realize the Swift book provided an implementation of a random number generator. Is the best practice to copy and paste this implementation in one's own program? Or is there a library that does this that we can use now?
相关问题
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
- Get the NSRange for the visible text after scroll
- UIPanGestureRecognizer is not working in iOS 13
- What does a Firebase observer actually do?
相关文章
- Using if let syntax in switch statement
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- why 48 bit seed in util Random class?
- Is there a Github markdown language identifier for
- How can I vertically align my status bar item text
- Adding TapGestureRecognizer to UILabel in Swift
- Attempt to present UIAlertController on View Contr
Edit: Updated for Swift 3.0
arc4random
works well in Swift, but the base functions are limited to 32-bit integer types (Int
is 64-bit on iPhone 5S and modern Macs). Here's a generic function for a random number of a type expressible by an integer literal:We can use this new generic function to extend
UInt64
, adding boundary arguments and mitigating modulo bias. (This is lifted straight from arc4random.c)With that we can extend
Int64
for the same arguments, dealing with overflow:To complete the family...
After all that, we can finally do something like this:
Use
arc4random_uniform()
Usage:
arc4random_uniform(someNumber: UInt32) -> UInt32
This gives you random integers in the range
0
tosomeNumber - 1
.The maximum value for
UInt32
is 4,294,967,295 (that is,2^32 - 1
).Examples:
Coin flip
Dice roll
Random day in October
Random year in the 1990s
General form:
where
number
,max
, andmin
areUInt32
.What about...
arc4random()
You can also get a random number by using
arc4random()
, which produces aUInt32
between 0 and 2^32-1. Thus to get a random number between0
andx-1
, you can divide it byx
and take the remainder. Or in other words, use the Remainder Operator (%):However, this produces the slight modulo bias (see also here and here), so that is why
arc4random_uniform()
is recommended.Converting to and from
Int
Normally it would be fine to do something like this in order to convert back and forth between
Int
andUInt32
:The problem, though, is that
Int
has a range of-2,147,483,648...2,147,483,647
on 32 bit systems and a range of-9,223,372,036,854,775,808...9,223,372,036,854,775,807
on 64 bit systems. Compare this to theUInt32
range of0...4,294,967,295
. TheU
ofUInt32
means unsigned.Consider the following errors:
So you just need to be sure that your input parameters are within the
UInt32
range and that you don't need an output that is outside of that range either.I've been able to just use
rand()
to get a random CInt. You can make it an Int by using something like this:You can use your favourite C random function, and just convert to value to Int if needed.
Swift 4.2
Swift 4.2 has included a native and fairly full-featured random number API in the standard library. (Swift Evolution proposal SE-0202)
All number types have the static random(in:) which takes the range and returns the random number in the given range
Swift 4.2
Bye bye to import Foundation C lib
arc4random_uniform()
More @ Official