0

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?

Bennnn
  • 17
  • 2

2 Answers2

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,759
  • 9
  • 68
  • 78
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 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
  • 6,257
  • 4
  • 36
  • 48