How do I generate a random number in Swift language that can be used in math, such as addition. Basically I just want to be able to generate a random number and then add it to a count that I have. For example, how would I generate a random number that I can add 1 to?
Asked
Active
Viewed 793 times
2 Answers
0
Try to use this one & the arc4random function will generate value between 1-9 & it returns UInt32 type value so modify it what you wanna.
var count : UInt32 = 10
var value : UInt32 = arc4random()%10
count += value
print(count)
ABakerSmith
- 22,443
- 9
- 67
- 78
Dharmbir Singh
- 17,277
- 5
- 49
- 66
0
Use arc4random_uniform() function because it generates a uniform distribution.
The following line generates a number from 0-9.
var x = Int(arc4random_uniform(10)) // Cast it to Int because function returns UInt32
println(x)
var sum = 10 + x
println(sum)
Ankit Goel
- 5,927
- 4
- 34
- 45