逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::
You should use the arc4random() function. It uses a superior algorithm to rand. You don't even need to set a seed.
arc4random, arc4random_stir, arc4random_addrandom -- arc4 random number generator

Same as C, you would do
int r = rand() % 74; // [0, 74)

Feel free to substitute random() or arc4random() for rand() (which is, as others have pointed out, quite sucky).

srandom((unsigned int)time(NULL)); // 防止出现警告



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

arc4random_uniform(upper_bound) avoids modulo bias as described in the man page:

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.


生成[0, 1)之间的随机数

arc4random_uniform(100) / 100.0

@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
posted on 2011-12-07 23:23 逛奔的蜗牛 阅读(1289) 评论(0)  编辑 收藏 引用 所属分类: Cocoa

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理