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?

0

2 Answers 2

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) 
Sign up to request clarification or add additional context in comments.

Comments

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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.