如何在iPhone上生成随机数? [英] How do I generate random numbers on the iPhone?

查看:380
本文介绍了如何在iPhone上生成随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iPhone上使用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 the iPhone?
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.

推荐答案

有一个非常类似的问题这里是StackOverFlow 。这是一个更好的解决方案(不需要播种):

There is a very similar question here on StackOverFlow. Here is one of the better solutions (no need for seeding):

int r = arc4random() % 5;

这篇关于如何在iPhone上生成随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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