开发者

Generate Random Number from fix Set of numbers in iphone

开发者 https://www.devze.com 2023-01-01 10:00 出处:网络
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,

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...


arc4random%(set count) = a random index.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消