# 在Objective-C中生成随机数字

``Random.nextInt(74)` `

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

` `arc4random_uniform(74)` `

`arc4random_uniform(upper_bound)`避免了手册页中描述的模偏差 ：

arc4random_uniform（）将返回一个小于upper_bound的均匀分布的随机数。 arc4random_uniform（）build议比arc4random（）％upper_bound''这样的结构更好，因为当上限不是2的幂时，它避免了“ 模偏置 ”。

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

（假设你的意思是包括0，但不包括74，这是你的Java例子）

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

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

` `- (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` `

` `#import <stdlib.h> #import <time.h> srandom(time(NULL)); random() % 74;` `

` `#include <stdlib.h> int r = 0; if (arc4random_uniform != NULL) r = arc4random_uniform (74); else r = (arc4random() % 74);` `

` `//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;` `

`arc4random_uniform(75)`

` `int x = arc4random()%100;` `

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

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

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

GameplayKit有一些便利的构造函数，可以与骰子一起工作。 例如，你可以像这样掷出一个六面骰子：

` `GKRandomDistribution *d6 = [GKRandomDistribution d6]; [d6 nextInt];` `

` `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;}` `