Access random items from an array in the iPhone SDK

南笙酒味 提交于 2019-12-22 13:59:03

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!