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

Random.nextInt(74)

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


当前回答

您应该使用arc4random_uniform()函数。它使用了一种高级算法来标记。你甚至不需要设置种子。

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

arc4random手册页:

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))

其他回答

和C一样

#include <time.h>
#include <stdlib.h>
...
srand(time(NULL));
int r = rand() % 74;

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

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

您应该使用arc4random_uniform()函数。它使用了一种高级算法来标记。你甚至不需要设置种子。

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

arc4random手册页:

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))

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

int x = arc4random()%100;

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

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

这会给你一个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);

我编写了自己的随机数实用程序类,这样我的功能就更像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(不包含)之间的随机数。