我主要是Java开发者,希望找到一种方法来生成0到74之间的伪随机数。在Java中,我会使用以下方法:
Random.nextInt(74)
我对种子或真正的随机性的讨论不感兴趣,只想知道如何在Objective-C中完成同样的任务。我已经在Google上搜索了很多,但似乎有很多不同和相互矛盾的信息。
我主要是Java开发者,希望找到一种方法来生成0到74之间的伪随机数。在Java中,我会使用以下方法:
Random.nextInt(74)
我对种子或真正的随机性的讨论不感兴趣,只想知道如何在Objective-C中完成同样的任务。我已经在Google上搜索了很多,但似乎有很多不同和相互矛盾的信息。
你应该使用arc4random_uniform()
函数。它使用比rand
更优秀的算法。你甚至不需要设置一个种子。
#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))
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_3
若未通过此检查,请使用其他解决方案。 - Ron和 C 一样,你会这样做
#include <time.h>
#include <stdlib.h>
...
srand(time(NULL));
int r = rand() % 74;
(假设您的意思是包括0但不包括74,这正是您的Java示例所做的)
编辑:请随意将random()
或arc4random()
替换为rand()
(正如其他人指出的那样,它非常糟糕)。
我认为我可以添加一个我在许多项目中使用的方法。
- (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))
例如。
NSInteger myInteger = RAND_FROM_TO(0, 74) // 0, 1, 2,..., 73, 74
注意:仅适用于 iOS 4.3 / OS X v10.7(狮子)及更高版本
这将给你一个在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);
根据rand(3)的手册页面,rand系列函数已经被random(3)取代。这是因为rand()的低12位会形成循环模式。要获取随机数,只需使用无符号种子调用srandom()生成器,并调用random()。因此,上面代码的等价于:
#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
的最佳方法是像这样进行:
#include <stdlib.h>
int r = 0;
if (arc4random_uniform != NULL)
r = arc4random_uniform (74);
else
r = (arc4random() % 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(不包括)之间的随机数字。
已经有很好的、简明扼要的回答,但问题是要求生成0到74之间的随机数。可使用:
arc4random_uniform(75)
NSInteger rand = [[GKRandomSource sharedRandom] nextInt];
这将生成一个介于-2,147,483,648和2,147,483,647之间的数字。如果您想要一个介于0和上限(不包括)之间的数字,您可以使用以下代码:
NSInteger rand6 = [[GKRandomSource sharedRandom] nextIntWithUpperBound:6];
GameplayKit中有一些方便的构造函数可用于处理骰子。例如,您可以像这样掷一个六面骰子:
GKRandomDistribution *d6 = [GKRandomDistribution d6];
[d6 nextInt];
而且你可以使用GKShuffledDistribution
等工具来调整随机分布。
arc4random_uniform(x)
。它也在stdlib.h中(在OS X 10.7和iOS 4.3之后),可以给出更均匀分布的随机数。用法为int r = arc4random_uniform(74);
。 - LavaSlider