在Objective-C中生成随机数

Generating random numbers in Objective-C

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

1
Random.nextInt(74)

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


您应该使用arc4random_uniform()函数。它使用了比rand更好的算法。你甚至不需要播种。

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

arc4random手册页:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
NAME
     arc4random, arc4random_stir, arc4random_addrandom -- arc4 random number generator

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <stdlib.h>

     u_int32_t
     arc4random(void);

     void
     arc4random_stir(void);

     void
     arc4random_addrandom(unsigned char *dat, int datlen);

DESCRIPTION
     The arc4random() function uses the key stream generator employed by the arc4 cipher, which uses 8*8 8
     bit S-Boxes.  The S-Boxes can be in about (2**1700) states.  The arc4random() function returns pseudo-
     random numbers in the range of 0 to (2**32)-1, and therefore has twice the range of rand(3) and
     random(3).

     The arc4random_stir() function reads data from /dev/urandom and uses it to permute the S-Boxes via
     arc4random_addrandom().

     There is no need to call arc4random_stir() before using arc4random(), since arc4random() automatically
     initializes itself.

EXAMPLES
     The following produces a drop-in replacement for the traditional rand() and random() functions using
     arc4random():

           #define foo4random() (arc4random() % ((unsigned)RAND_MAX + 1))


使用arc4random_uniform(upper_bound)函数生成一个范围内的随机数。下面将生成一个介于0和73之间(含0和73)的数字。

1
arc4random_uniform(74)

arc4random_uniform(upper_bound)避免模块偏差,如手册页所述:

arc4random_uniform() will return a uniformly distributed random number less than upper_bound. arc4random_uniform() is recommended over constructions like ``arc4random() % upper_bound'' as it avoids"modulo bias" when the upper bound is not a power of two.


和C一样,你会的

1
2
3
4
5
#include <time.h>
#include <stdlib.h>
...
srand(time(NULL));
int r = rand() % 74;

(假设您的意思是包括0个,但不包括74个,这就是Java示例所做的)

编辑:可以用random()arc4random()代替rand()(正如其他人指出的,这很糟糕)。


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

1
2
3
- (NSInteger)randomValueBetween:(NSInteger)min and:(NSInteger)max {
    return (NSInteger)(min + arc4random_uniform(max - min + 1));
}

如果我最终在许多文件中使用它,我通常将宏声明为

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

例如。

1
NSInteger myInteger = RAND_FROM_TO(0, 74) // 0, 1, 2,..., 73, 74

注意:仅适用于iOS 4.3/OS x v10.7(lion)及更高版本


这将给您一个介于0和47之间的浮点数

1
2
3
float low_bound = 0;      
float high_bound = 47;
float rndValue = (((float)arc4random()/0x100000000)*(high_bound-low_bound)+low_bound);

或者只是简单地

1
float rndValue = (((float)arc4random()/0x100000000)*47);

下限和上限也可以是负数。下面的示例代码提供了一个介于-35.76和+12.09之间的随机数

1
2
3
float low_bound = -35.76;      
float high_bound = 12.09;
float rndValue = (((float)arc4random()/0x100000000)*(high_bound-low_bound)+low_bound);

将结果转换为更圆整数值:

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


根据RAND(3)手册页,RAND系列函数已被随机(3)废弃。这是因为rand()的较低12位通过循环模式。要获得随机数,只需使用无符号种子调用srandom()来为生成器种子,然后调用random()。因此,上面代码的等价物是

1
2
3
4
5
#import <stdlib.h>
#import <time.h>

srandom(time(NULL));
random() % 74;

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


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

确定arc4random_uniform是否可用的最佳方法是这样做:

1
2
3
4
5
6
7
#include <stdlib.h>

int r = 0;
if (arc4random_uniform != NULL)
    r = arc4random_uniform (74);
else
    r = (arc4random() % 74);


我编写了我自己的随机数实用工具类,这样我就可以在Java中运行一些更像Max.Read()的功能。它只有两个功能,都是用C做的。

头文件:

1
2
3
//Random.h
void initRandomSeed(long firstSeed);
float nextRandomFloat();

实施文件:

1
2
3
4
5
6
7
8
9
10
11
12
//Random.m
static unsigned long seed;

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

float nextRandomFloat()
{
    return (((seed= 1664525*seed + 1013904223)>>16) / (float)0x10000);
}

这是生成伪随机数的一种非常经典的方法。在我的应用程序代理中,我打电话给:

1
2
3
4
5
6
7
#import"Random.h"

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

后来我说:

1
float myRandomNumber = nextRandomFloat() * 74;

请注意,此方法返回一个介于0.0f(含)和1.0f(不含)之间的随机数。


已经有一些很好的,清晰的答案,但是这个问题要求0到74之间的随机数。用途:

arc4random_uniform(75)


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

您可以从四种源类型中进行选择:常规随机源(未命名,直至系统选择它的功能)、线性同余、arc4和mersenne twister。它们可以生成随机的int、float和bools。

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

1
NSInteger rand = [[GKRandomSource sharedRandom] nextInt];

它生成一个介于-2147483648和2147483647之间的数字。如果您想要一个介于0和上界(独占)之间的数字,您可以使用:

1
NSInteger rand6 = [[GKRandomSource sharedRandom] nextIntWithUpperBound:6];

gameplaykit内置了一些方便的构造器来处理骰子。例如,可以像这样滚动六边形模具:

1
2
GKRandomDistribution *d6 = [GKRandomDistribution d6];
[d6 nextInt];

另外,您可以使用GKShuffledDistribution这样的东西来形成随机分布。


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

1
2
3
4
5
6
7
8
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

  • 随机数步骤2:52


生成0到99之间的随机数:

1
int x = arc4random()%100;

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

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

对于游戏开发人员,使用random()生成random。可能比使用arc4random()快至少5倍。当使用全范围的random()生成random时,模块偏差不是一个问题,尤其是对于游戏而言。一定要先播种。在AppDelegate中调用srandomdev()。以下是一些助手函数:

1
2
3
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;}