Tamarisk
Tamarisk

Reputation: 939

Generate random integer from discrete selection?

I am trying to generate random numbers but only certain numbers. I know to generate a random number between 0 and 10 you'd use:

arc4random_uniform(11)

But what if I wanted to generate a random number between a selection of, say 3, 5, 8, and 10?

Upvotes: 2

Views: 54

Answers (1)

whereisleo
whereisleo

Reputation: 828

Vacawama is right and should be given credit.

a little more thought.

Chose what number you want and put them into an array. then use the index of the array to get the

[3, 5, 8, 10]

array index starts at zero so; [0: 3, 1: 5, 2: 8, 3: 10].

using "4" within the arc4random will let you choose between 0-3.

Upvotes: 2

Related Questions