Random number is the unprecedented and unpredictable, Sometimes we use Random number to choose instances in Games or is Arcade game or poping-out the blocks from block game.
In game development, you use random numbers to simulate dice rolls, or even simulate “chaos” and unpredictability in your games, like random enemy positions or random upgrade spawns.
Random Number Functions In Swift 4.2+
In Swift 4.2. and after, the way you work with random numbers has changed. Instead of using the imported C function arc4random(), you can now use Swift’s own native functions.
let number = Int.random(in: 0 ..< 10)
The above example generates a random integer number between 0 and 10. The half-open range operator ..< is used, and the result is a range that runs from 0 to 10, not including 10.
let fraction = Float.random(in: 0 ..< 1)
The above example returns a random floating-point value, i.e. a number with a fraction, between 0 and 1.
And this example either returns true or false – a random boolean!
let stayOrGo = Bool.random()
What about picking a random element from an array? You can do that like this:
let names = [“A”, “B”, “C”, “D”, “E”]
let randomName = names.randomElement()
In the above code you use randomElement() on the names array. You can use this function on any Collection, such as arrays and dictionaries. Keep in mind that the returned random element is an optional.
Randomizing the order of an array is surprisingly simple:
let names = ["A", "B", "C", "D", "E"]
// 'names' can now be: ["B", "E", "D", "A", "C"]
The shuffle functions use Swift’s typical naming structure, so shuffle() shuffles the array in-place, and shuffled() returns a copy of the shuffled array.
And you can even shuffle a Sequence, like this:
let sequence = 0 ..< 7
let shuffledSequence = sequence.shuffled()
// 'shuffledSequence' can now be: [0, 6, 2, 3, 4, 1, 5]
Easy, right? Much easier than the pre-4.2 arc4random_uniform(_:) with all that type casting…
Random Number Functions Before Swift 4.2
Swift has three typical functions for random numbers:
arc4random() returns a random number between zero and 232–1
arc4random_uniform(_:) returns a random number between zero and the first parameter, minus one.
drand48() returns a random Double between 0.0 and 1.0
Both arc4random() and arc4random_uniform(_:) use the UInt32 type instead of the more typical Int.
Random Numbers with arc4random_uniform(_:)
The function arc4random_uniform(_:) takes one parameter, the upper bound. It’ll return a random number between 0 and this upper bound, minus 1.
// Output: 13
This will return a random number between 0 and 41. The result is of type UInt32, so if you want to work with an ordinary integer in your code, you’ll have to convert or type cast it to Int.
let n = Int(arc4random_uniform(42))
Random Doubles with drand48()
What about double’s? As you know, a double is a decimal-point number with double precision. It’s most often used in Swift for number’s that have commas , or fractions.
This is how you generate a random double in Swift, between 0.0 and 1.0:
let d = drand48()
// Output: 0.396464773760275