随机数函数哑火 [英] Random number function is misfiring

查看:103
本文介绍了随机数函数哑火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要从1-100的随机整数一个非常简单的iPhone应用程序。

I have a very simple iPhone app that requires a random integer from 1-100.

我有一个调用随机数函数,那么一个按钮显示出来。

I have a button that calls the random number function then displays it.

-(IBAction)buttonReleased;
{
    srandom(time(NULL)); 
    int theNum = random() % 100 + 1;
    numberDisplay.text = [NSString stringWithFormat:@"%d", theNum];
}

问题是,如果我preSS按钮很快,有时则不会显示一个新的随机数。

The problem is, if I press the button quickly, sometimes it won't display a new random number.

推荐答案

问题是你与时间播种。

时间仅每秒更新一次,所以如果你在一秒内点击它,你会播种发生器用相同的号码,这意味着你会得到的相同的编号。

time is only updated every second, so if you click it within the second, you will seed the generator with the same number, which means you'll be getting the same number.

您应该只播种一次,在应用程序的开始。

You should only be seeding once, at the start of the application.

这篇关于随机数函数哑火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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