如何在iOS上生成随机数? [英] How do I generate random numbers on iOS?
本文介绍了如何在iOS上生成随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在iOS上使用Objective-C生成随机数的最佳方法是什么?
如果我使用(int)((double) rand() / ((double)(RAND_MAX) + (double) 1) * 5.0)
生成一个0到4之间的数字,则每次在iPhone上启动该程序时,它都会以相同的数字开始.
What is the best way to generate random numbers using Objective-C on iOS?
If I use (int)((double) rand() / ((double)(RAND_MAX) + (double) 1) * 5.0)
to generate a number from 0 to 4, every time I start the program on the iPhone it generates the same numbers to start off with.
推荐答案
There is a very similar question here on StackOverFlow. Here is one of the better solutions (no need for seeding):
int r = arc4random() % 5;
这篇关于如何在iOS上生成随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文