用C随机数发生器 [英] Random Number Generator in C

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

问题描述

我想产生一个随机数0 - 59和很不满意,在C.这里rand()函数是code我与玩弄:

 的#include<&stdlib.h中GT;
#包括LT&;&time.h中GT;主要()
{INT NUM;
函数srand(时间(NULL));
NUM = RAND();
NUM = NUM​​%59;
的printf(%d个\\ N,NUM);
}

我已经多次在这个code运行,发现所产生的随机数并不真正似乎是随机的。产生的产生的数字肯定是下面的一种模式,因为我每次运行程序的数量变得逐渐变大,直至绕回至开始(时间,即2,17,21,29,38,47,54,59,4, 11 ....等)。

有没有一种方法可以让我播种的功能,所以我每次重新运行功能时,我得到了1/60的机会正在产生一个真正的随机数?还是有我可以实现我自己,而不是使用在C rand()函数的替代方法?


解决方案

  

有没有一种方法可以让我播种的功能,所以每次我的时间重新运行我得到一个真正的随机数的函数


没有,C标准库使用PRNG(伪随机数生成器)。你将永远不会得到真正随机数。

可以,但是,与一些比的变化更频繁的时间(),例如,在POSIX它的种子

  timeval结构TM;
函数gettimeofday(安培;以旧换新,NULL);
srandom(tm.tv_sec + tm.tv_usec * 1000000ul);

此外,使用模运算符用于产生随机数不是一个好的解决方案(它严重降低熵)。如果你有一个BSD风格的libc实现,用

  uint32_t的N = arc4random_uniform(60);

或者,如果你不具备这个功能:

  //随机的()是保证在区间[0返回一个数字... 2 ** 31)
的#define MAX_RANDOM((1 <<;&下; 31) - 1)N久;做{
    N =随机();
}而(正&GT;(MAX_RANDOM - ((MAX_RANDOM%60)+ 1)%60));
N%= 60;

请注意使用随机() - 它优于兰特()(其中有且具有低质量的实现数)。此功能可以使用接种 srandom()


  

还是有我可以实现我自己,而不是在C语言中使用rand()函数?任何替代方法


您可以(当然,要不怎么会在C库实现的作家呢?),但你最好不要 - 它是一个独立的科学写的好PRNG,所以说

I'm trying to generate a random number 0 - 59, and am not satisfied with the rand() function in C. Here is the code I'm playing around with:

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

main()
{

int num;
srand(time(NULL));
num = rand();
num = num % 59;
printf("%d\n", num);
}

I've repeated the run on this code and noticed that the random numbers being generated don't really seem that random. The generated numbers produced are definitely following a pattern, as each time I run the program the number gets progressively larger until it wraps back around to the beginning (i.e. 2, 17, 21, 29, 38, 47, 54, 59, 4, 11....etc).

Is there a way I can seed the function so that every time I re-run the function I get a true random number with a 1/60 chance of being generated? Or are there any alternative methods I can implement myself rather than using the rand() function in C?

解决方案

Is there a way I can seed the function so that every time I re-run the function I get a true random number

No, the C standard library uses a PRNG (pseudorandom number generator). You will never get true random numbers.

You can, however, seed it with something that changes more frequently than time(), for example, on POSIX:

struct timeval tm;
gettimeofday(&tm, NULL);
srandom(tm.tv_sec + tm.tv_usec * 1000000ul);

Also, using the modulo operator for generating a random number is not a good solution (it severely decreases entropy). If you have a BSD-style libc implementation, use

uint32_t n = arc4random_uniform(60);

Or, if you don't have this function:

// random() is guaranteed to return a number in the range [0 ... 2 ** 31)
#define MAX_RANDOM ((1 << 31) - 1)

long n;

do {
    n = random();
} while (n > (MAX_RANDOM - ((MAX_RANDOM % 60) + 1) % 60));
n %= 60;

Note the use of random() - it is superior to rand() (which had and has a number of low-quality implementations). This function can be seeded using srandom().

Or are there any alternative methods I can implement myself rather than using the rand() function in C?

You can (of course, else how would the writers of the C library implementation do it?), but you better not - it's a separate science to write a good PRNG, so to say.

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

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