Generate Random Number from fix Set of numbers in

2019-07-04 23:51发布

问题:

Suppose I have One set of numbers i.e {1, 6, 3, 5, 7, 9} I want to Generate Random number from this set of number only i.e. a Generated number should be random and should be from these number({1, 6, 3, 5, 7, 9}) only.

standard C/C++ function will also do...

回答1:

arc4random%(set count) = a random index.



回答2:

What they are telling you is this. Generate a random number from 0-5. Then use that as an index into the array. Eg if the random # is 2, look at element #2 (the third one since you start at 0) of your list of numbers, which is 3. If the random # is 5, you get 9.

MSalters' comment shows you how to do it in a single expression.