I am new to iPhone programming. I have 10 number say (1,2,3,4,5,6,7,8,9,10). I want to choose randomly 1 number from the above 10 numbers. How can I choose a random number from a set of numbers?
I am new to iPhone programming. I have 10 number say (1,2,3,4,5,6,7,8,9,10) . I
Share
If you simply want a value between 1 and 10, you can use the standard C
rand()method. This returns an integer between zero and RAND_MAX.To get a value between 0 and 9 you can use the
%operator. So to get a value between 1 and 10 you can use:If you don’t want the same series of pseudo random numbers each time, you’ll need to use
srandto seed the random number generator. A good value to seed it with would be the current time.If you’re asking about choosing a number from a list of arbitrary (and possibly non consecutive) numbers, you could use the following.