Random Number Generator with Swift
whatis.techtarget.com defines random numbers as numbers that occur in a sequence such that two conditions are met:
- The values are uniformly distributed over a defined interval or set, and
- It is impossible to predict future values based on past or present ones.
In Swift 4.2 random numbers can be generated via several functions. For example
- arc4Random()
- arc4RandomUniform()
For those of you familiar with C programming this will be a very basic topic. However, if you’re not familiar with C here’s what both of them stand for :
arc4Random() returns an integer between 0 and (2³²)-1
arc4random_uniform() will return a uniformly distributed random number less than upper_bound that you pass into it.
Okay, enough with the theory behind the function. Let’s write some code.
Swift Playground
In your Xcode Playground start by clearing out all the pre-written code.
Next, import any of these libraries
- Appkit
- UIKit
- Foundation
Both the random functions are part of the Darwin library which is automatically imported if you import any of the libraries mentioned above.
Next, let’s declare a variable that will contain our random number. Let’s call this randomNumber and let’s say this variable an initial value of 0. Swift has type inference so it’ll automatically figure out that we want the variable randomNumber to contain the datatype of integer.
Now, let’s mutate the values in the variable by calling the arc4RandomUniform function.
Equate your randomNumber variable to the arc4random_uniform() function.
When you type your code out you’ll notice that Xcode will autocomplete and notify you that the parameter you need to pass to the function is the upper bound which is of type UInt32 which will cause some problems.
Initially, we declared to XCode that our variable is of type Int and now we’re trying to pass a UInt32 to it. This will cause what is known as a type mismatch and needs to be rectified if we’re to compile our code successfully.
How do you fix a type mismatch?
To fix this issue add the keyword Int in front of the function and close the function within two round brackets. This will do a type conversion and the right-hand side of the equation will change to an integer value that the variable randomNumber will be willing to accept.
To display the code you can use the print command in Swift and you’ll see that a random number between 0 and you’re upper bound — 1 is generated and displayed in the console.
But why UpperBound — 1?
For those of you new to programming this might seem a rather odd issue but it’s based on the very basic principle of how a computer works. In an array, which is a collection of similar datatype elements, each element is given an address and the addresses start from the number 0. Our Random Function is also pretty much an array that looks for a random number in the array of numbers that have been passed into it. Therefore, in pretty much every step in programming counting begins from the number 0 and hence the range of a value is from 0 to final number -1
If you have any questions feel free to ping me @SwapnanilDhol on Twitter !