题
我Java头的主要是,并且我想要的方式生成一个伪随机数量在0和74。在Java我将使用的方法:
Random.nextInt(74)
我没兴趣讨论有关种子或真正随机性的,只是你如何完成相同任务的目标。我已经走遍了谷歌,那里似乎只是以许多不同的和相互冲突的位的信息。
解决方案
您应该使用 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))
其他提示
与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))
E.g。
NSInteger myInteger = RAND_FROM_TO(0, 74) // 0, 1, 2,..., 73, 74
注意:仅适用于iOS 4.3 / OS&nbsp; X&nbsp; v10.7(Lion)及更高版本
这将为您提供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系列函数已被随机淘汰(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);
}
这是产生伪随机数的一种非常经典的方法。在我的app委托中,我打电话给:
#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)代码>
从iOS 9和OS X 10.11开始,您可以使用新的GameplayKit类以多种方式生成随机数。
您有四种源类型可供选择:一般随机源(未命名,下至系统选择它的作用),线性同余,ARC4和Mersenne Twister。这些可以产生随机的整数,浮点数和布尔值。
在最简单的级别,您可以从系统的内置随机源生成一个随机数,如下所示:
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
之类的内容来塑造随机分布。
生成0到99之间的随机数:
int x = arc4random()%100;
生成500到1000之间的随机数:
int x = (arc4random()%501) + 500;
//下面的例子是,要产生数量在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
随机数第2步: 52
对于游戏开发使用random()生成randoms。可能比使用arc4random()快至少5倍。 Modulo偏差不是问题,特别是对于游戏,使用全范围的random()生成randoms。一定要先播种。在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;}