在 C 中生成随机数 [英] Generating random numbers in C

查看:35
本文介绍了在 C 中生成随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在搜索关于在 CI 中生成随机数的教程时发现了这个主题

While searching for Tutorials on generating random numbers in C I found this topic

当我尝试使用不带参数的 rand() 函数时,我总是得到 0.当我尝试使用带参数的 rand() 函数时,我总是得到值 41.每当我尝试使用 arc4random()random() 函数时,我都会收到 LNK2019 错误.

When I try to use the rand() function without parameters, I always get 0. When I try to use the rand() function with parameters, I always get the value 41. And whenever I try to use arc4random() and random() functions, I get a LNK2019 error.

这是我所做的:

#include <stdlib.h>
int main()
{
  int x;
  x = rand(6);
  printf("%d", x);
}

这段代码总是生成 41.我哪里出错了?我正在运行 Windows XP SP3 并使用 VS2010 命令提示符作为编译器.

This code always generates 41. Where am I going wrong? I'm running Windows XP SP3 and using VS2010 Command Prompt as compiler.

推荐答案

你应该在调用 rand 之前调用 srand() 来初始化随机数生成器.

You should call srand() before calling rand to initialize the random number generator.

要么用特定的种子调用它,你总会得到相同的伪随机序列

Either call it with a specific seed, and you will always get the same pseudo-random sequence

#include <stdlib.h>

int main ()
{
  srand ( 123 );
  int random_number = rand();
  return 0;
}

或使用不断变化的来源调用它,即时间函数

or call it with a changing sources, ie the time function

#include <stdlib.h>
#include <time.h>

int main ()
{
  srand ( time(NULL) );
  int random_number = rand();
  return 0;
}

回应月亮的评论rand() 生成一个随机数,其概率在 0 和 RAND_MAX 之间(stdlib.h 中预定义的宏)

In response to Moon's Comment rand() generates a random number with an equal probability between 0 and RAND_MAX (a macro pre-defined in stdlib.h)

然后您可以将此值映射到较小的范围,例如

You can then map this value to a smaller range, e.g.

int random_value = rand(); //between 0 and RAND_MAX

//you can mod the result
int N = 33;
int rand_capped = random_value % N;  //between 0 and 32
int S = 50;
int rand_range = rand_capped + S; //between 50 and 82

//you can convert it to a float
float unit_random = random_value / (float) RAND_MAX; //between 0 and 1 (floating point)

这对于大多数用途来说可能已经足够了,但值得指出的是,在第一种情况下,如果 N 没有被均匀地划分为 RAND_MAX+1,则使用 mod 运算符会引入轻微的偏差.

This might be sufficient for most uses, but its worth pointing out that in the first case using the mod operator introduces a slight bias if N does not divide evenly into RAND_MAX+1.

随机数生成器很有趣也很复杂,大家普遍认为C标准库中的rand()生成器不是一个质量很好的随机数生成器,阅读(http://en.wikipedia.org/wiki/Random_number_generation 了解质量的定义).

Random number generators are interesting and complex, it is widely said that the rand() generator in the C standard library is not a great quality random number generator, read (http://en.wikipedia.org/wiki/Random_number_generation for a definition of quality).

http://en.wikipedia.org/wiki/Mersenne_twister(来源 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html ) 是一种流行的高质量随机数生成器.

http://en.wikipedia.org/wiki/Mersenne_twister (source http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html ) is a popular high quality random number generator.

另外,我不知道 arc4rand() 或 random() 所以我不能评论.

Also, I am not aware of arc4rand() or random() so I cannot comment.

这篇关于在 C 中生成随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆