2012年10月16日 星期二

objectC下的亂數取用

參考: http://www.cnblogs.com/xuling/archive/2012/02/28/2370692.html

在Xcode經常需要使用到亂數來取隨意值變化,基本上有三種選擇


1)、arc4random() 比較精確不需要生成隨即種子
使用方法 :
通過arc4random() 獲取0到x-1之間的整數的代碼如下:
int value = arc4random() % x;

獲取1到x之間的整數的代碼如下:
int value = (arc4random() % x) + 1;

浮點數取值
#define ARC4RANDOM_MAX      0x100000000

Then, you can use arc4random() to get a floating point value (at double the precision of using rand()), between 0 and 100, like so:
double val = floorf(((double)arc4random() / ARC4RANDOM_MAX) * 100.0f);





arc4random,它就藏在C语言标准库(Standard C Library)当中.文档对于它的描述是:
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 (21700) states. The arc4random() function returns pseudo-random numbers in the range of 0 to (232)-1, and therefore has twice the range of rand(3) and random(3) .
arc4random既使用了arc4加密算法避免seed重复,并且比random的取值范围(2**31)-1整整大了一倍



在iPhone中,RAND_MAX是0x7fffffff (2147483647),而arc4random()返回的最大值则是 0×100000000 (4294967296),从而有更好的精度。此外,使用arc4random()还不需要生成随机种子,因为第一次调用的时候就会自动生成。


2)、CCRANDOM_0_1() cocos2d中使用 ,範圍是[0,1]
使用方法:
float random = CCRANDOM_0_1() * 5; //[0,5] CCRANDOM_0_1() 取值範圍是[0,1]

網路上的有資料說明  這是一個random的定義微碼
#define CCRANDOM_0_1() ((random() / (float)0x7fffffff ))



所以
int i = CCRANDOM_0_1();
 
==> int i =  ((random() / (float)0x7fffffff ));
 






3)、random() 需要初始化時設置種子
使用方法:
srandom((unsigned int)time(time_t *)NULL); //初始化時,設置下種子就好了。




補充資料,arc4random有一些變形的函數可用。

ARC4RANDOM(3)            BSD Library Functions Manual            ARC4RANDOM(3)

NAME
     arc4random, arc4random_buf, arc4random_uniform, 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_buf(void *buf, size_t nbytes);

     u_int32_t
arc4random_uniform(u_int32_t upper_bound);

arc4random_uniform(74); // [0, 74)


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

     arc4random_buf() function fills the region buf of length nbytes with ARC4-derived random data.

     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.

     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() functions family, since they automatically initialize themselves.

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

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

SEE ALSO

HISTORY
     RC4 has been designed by RSA Data Security, Inc.  It was posted anonymously to the USENET and was confirmed to be equivalent by several sources who had access to the
     original cipher.  Since RC4 used to be a trade secret, the cipher is now referred to as ARC4.

BSD                             April 15, 1997                             BSD



Xcode下使用範例
在UIView下使用一個Button及Label

然後使用一個IBAction如下,就會得到變數,1~6
- (IBAction)getRandom:(id)sender {
   
    int value = arc4random() % 6 +1;
   
    numLabel.text = [NSString stringWithFormat:@"%d",value];
   
}

網路上有人已經有一個例子可供參考
http://divakalife.blogspot.tw/2011/01/iphone-app-random-number.html

沒有留言: