从 iPhone SDK 中的数组访问随机项目 [英] Access random items from an array in the iPhone SDK

查看:48
本文介绍了从 iPhone SDK 中的数组访问随机项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在 iPhone SDK 中访问数组中的随机项?

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.

推荐答案

您可以使用 rand() 函数:

NSInteger randomIndex = rand () % [list count];
Thing * randomThing = [list objectAtIndex:randomIndex];

不要忘记在启动时调用 srand(unsigned int seed):

Don't forget to call srand(unsigned int seed) on startup:

srand(time(0)); 

这篇关于从 iPhone SDK 中的数组访问随机项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆