views:

273

answers:

2

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

+5  A: 

arc4random%(set count) = a random index.

JustSid
Not getting you... arc4random%6 will generate random numbers from 0 to 6 but i want to generate from a specific set.. i.e i want to generate random number either 1 3 or 5 not all number between 0 and 6... Thanks...
mihirpmehta
The return value is just an index in an array. This is how you generate a random number from an array. Generate a random index `a`, and take the element of index `a` from array.
Michał Trybus
i.e. `collection[rand() % collection.size()]`
MSalters
Ok got it... It was making perfect logical sense... Thanks a lot...
mihirpmehta
+2  A: 

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.

Kate Gregory
Thanks......... :)
mihirpmehta