问题
I want to know how to access random items from array in the iPhone SDK?
I have made a music player in which the next song comes from array on button-click, but I want to get the songs randomly.
回答1:
You can use the rand() function:
NSInteger randomIndex = rand () % [list count];
Thing * randomThing = [list objectAtIndex:randomIndex];
Don't forget to call srand(unsigned int seed) on startup:
srand(time(0));
来源:https://stackoverflow.com/questions/3509411/access-random-items-from-an-array-in-the-iphone-sdk