在Objective-C中生成随机数

在Objective-C中生成随机数,objective-c,random,Objective C,Random,我主要是一个Java高手,我想要一种生成0到74之间的伪随机数的方法。在Java中,我将使用以下方法: Random.nextInt(74) 我对关于种子或真正的随机性的讨论不感兴趣,只对你如何在Objective-C中完成同样的任务感兴趣。我搜索了谷歌,似乎有很多不同的、相互冲突的信息。和C一样,你也会这样做 #include <time.h> #include <stdlib.h> ... srand(time(NULL)); int r = rand() % 74

我主要是一个Java高手,我想要一种生成0到74之间的伪随机数的方法。在Java中,我将使用以下方法:

Random.nextInt(74)

我对关于种子或真正的随机性的讨论不感兴趣,只对你如何在Objective-C中完成同样的任务感兴趣。我搜索了谷歌,似乎有很多不同的、相互冲突的信息。

和C一样,你也会这样做

#include <time.h>
#include <stdlib.h>
...
srand(time(NULL));
int r = rand() % 74;
#包括
#包括
...
srand(时间(空));
int r=rand()%74;
(假设您的意思是包括0但不包括74,这就是您的Java示例所做的)


编辑:请随意用
random()
arc4random()
代替
rand()
(正如其他人所指出的,这非常糟糕)。

根据rand(3)的手册页,rand函数族已经被random(3)淘汰。这是因为rand()的低12位通过循环模式。要获得一个随机数,只需通过使用无符号种子调用srandom()对生成器进行种子设定,然后调用random()。因此,上述代码的等价物是

#import <stdlib.h>
#import <time.h>

srandom(time(NULL));
random() % 74;
#导入
#进口
srandom(时间(空));
随机()%74;

除非要更改种子,否则只需在程序中调用srandom()一次。虽然你说你不想讨论真正的随机值,但是rand()是一个非常糟糕的随机数生成器,random()仍然存在模偏差,因为它会生成一个介于0和rand_MAX之间的数。例如,如果rand_MAX是3,你想要一个介于0和2之间的随机数,您得到0的可能性是1或2的两倍。

您应该使用
arc4random\u uniform()
函数。它使用一种优于
rand
的算法。你甚至不需要播种

#include <stdlib.h>
// ...
// ...
int r = arc4random_uniform(74);

我编写了自己的随机数实用程序类,这样我就有了一些类似于Java中Math.random()的功能。它只有两个功能,都是用C语言编写的

头文件:

//Random.h
void initRandomSeed(long firstSeed);
float nextRandomFloat();
实施文件:

//Random.m
static unsigned long seed;

void initRandomSeed(long firstSeed)
{ 
    seed = firstSeed;
}

float nextRandomFloat()
{
    return (((seed= 1664525*seed + 1013904223)>>16) / (float)0x10000);
}
这是一种非常经典的生成伪随机数的方法。在我的应用程序中,我调用:

#import "Random.h"

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    initRandomSeed( (long) [[NSDate date] timeIntervalSince1970] );
    //Do other initialization junk.
}
后来我就说,

float myRandomNumber = nextRandomFloat() * 74;

请注意,此方法返回的随机数介于0.0f(包括)和1.0f(排除)之间。

使用
arc4random\u uniform(上限)
函数在一定范围内生成随机数。以下内容将生成一个介于0和73之间(含0和73)的数字

arc4random_uniform(74)
arc4random\u uniform(上限)

arc4random_uniform()将返回小于上限的均匀分布随机数。arc4random_uniform()建议使用在诸如``arc4random()%upper_bound''之类的构造上,因为当上界不是二的幂时,它可以避免使用“”


这将为您提供一个介于0和47之间的浮点值

float low_bound = 0;      
float high_bound = 47;
float rndValue = (((float)arc4random()/0x100000000)*(high_bound-low_bound)+low_bound);
或者干脆

float rndValue = (((float)arc4random()/0x100000000)*47);
下限和上限也可以是负值。下面的示例代码给出了一个介于-35.76和+12.09之间的随机数

float low_bound = -35.76;      
float high_bound = 12.09;
float rndValue = (((float)arc4random()/0x100000000)*(high_bound-low_bound)+low_bound);
将结果转换为更圆的整数:

int intRndValue = (int)(rndValue + 0.5);

最好使用
arc4random\u uniform
。但是,这在iOS 4.3下不可用。幸运的是,iOS将在运行时绑定这个符号,而不是在编译时(因此不要使用#if预处理器指令来检查它是否可用)

确定是否有
arc4random_uniform
可用的最佳方法是执行以下操作:

#include <stdlib.h>

int r = 0;
if (arc4random_uniform != NULL)
    r = arc4random_uniform (74);
else
    r = (arc4random() % 74);
NSInteger rand = [[GKRandomSource sharedRandom] nextInt];
GKRandomDistribution *d6 = [GKRandomDistribution d6];
[d6 nextInt];
#包括
int r=0;
if(arc4random_uniform!=NULL)
r=arc4random_均匀(74);
其他的
r=(arc4random()%74);

我想我可以添加一个在许多项目中使用的方法

- (NSInteger)randomValueBetween:(NSInteger)min and:(NSInteger)max {
    return (NSInteger)(min + arc4random_uniform(max - min + 1));
}
如果我在许多文件中使用它,我通常将宏声明为

#define RAND_FROM_TO(min, max) (min + arc4random_uniform(max - min + 1))
例如


注意:仅适用于iOS 4.3/OSXv10.7(Lion)及更高版本生成0到99之间的随机数:

int x = arc4random()%100;

生成500到1000之间的随机数:

int x = (arc4random()%501) + 500;

已经有一些很好的、清晰的答案了,但是这个问题需要一个介于0和74之间的随机数。使用:


arc4random\u uniform(75)

//下面的示例将生成一个介于0和73之间的数字

int value;
value = (arc4random() % 74);
NSLog(@"random number: %i ", value);

//In order to generate 1 to 73, do the following:
int value1;
value1 = (arc4random() % 73) + 1;
NSLog(@"random number step 2: %i ", value1);
输出:

  • 随机数:72

  • 随机数第二步:52


从iOS 9和OS X 10.11开始,您可以使用新的GameplayKit类以多种方式生成随机数

您有四种源类型可供选择:普通随机源(未命名,由系统选择其功能)、线性全等、ARC4和Mersenne Twister。这些可以生成随机整数、浮点和布尔

在最简单的级别上,您可以从系统的内置随机源生成一个随机数,如下所示:

#include <stdlib.h>

int r = 0;
if (arc4random_uniform != NULL)
    r = arc4random_uniform (74);
else
    r = (arc4random() % 74);
NSInteger rand = [[GKRandomSource sharedRandom] nextInt];
GKRandomDistribution *d6 = [GKRandomDistribution d6];
[d6 nextInt];
这将生成一个介于-2147483648和2147483647之间的数字。如果您想要一个介于0和上限(独占)之间的数字,您可以使用:

NSInteger rand6 = [[GKRandomSource sharedRandom] nextIntWithUpperBound:6];
GameplayKit内置了一些方便的构造函数来处理骰子。例如,您可以像这样滚动六面模具:

#include <stdlib.h>

int r = 0;
if (arc4random_uniform != NULL)
    r = arc4random_uniform (74);
else
    r = (arc4random() % 74);
NSInteger rand = [[GKRandomSource sharedRandom] nextInt];
GKRandomDistribution *d6 = [GKRandomDistribution d6];
[d6 nextInt];
此外,您还可以使用类似于
GKShuffledDistribution

的方法来塑造随机分布。对于游戏开发人员,请使用random()生成随机数。可能至少比使用arc4random()快5倍。模偏差不是一个问题,尤其是在游戏中,当使用全范围的随机数()生成随机数时。一定要先播种。在AppDelegate中调用srandomdev()。以下是一些帮助器函数:

static inline int random_range(int low, int high){ return (random()%(high-low+1))+low;}
static inline CGFloat frandom(){ return (CGFloat)random()/UINT32_C(0x7FFFFFFF);}
static inline CGFloat frandom_range(CGFloat low, CGFloat high){ return (high-low)*frandom()+low;}

您不妨调用srandomdev()而不是将时间传递给srandom();这同样简单,而且数学上更好。你需要为随机数生成器设定种子,否则每次执行时都会得到相同的数字模式。我想从一个不同于零的数字开始,怎么样?@amok:你只需将你想从中开始的数字添加到结果中,我就会一直得到数字9。我认为这是相当随机的;Di刚刚测试了random(),它显示了与rand()相同的问题使用
arc4random\u uniform(x)