不使用seed C编程的随机数

不使用seed C编程的随机数,c,random,C,Random,有没有办法不用seed在C中生成一个随机数 到目前为止,它仍然使用srand(time(NULL));这是一粒种子 #include <stdio.h> #include <time.h> #include <math.h> /* required for sqrt() */ #include <stdlib.h> /* required for rand() */ int gen_rand(); /* note these are decl

有没有办法不用seed在C中生成一个随机数

到目前为止,它仍然使用srand(time(NULL));这是一粒种子

#include <stdio.h>
#include <time.h>
#include <math.h> /* required for sqrt() */
#include <stdlib.h> /* required for rand() */

int gen_rand();   /* note these are declarations of functions */

void main()
{
   int number;
   srand (time(NULL)); /* everytime you run program, it will give you different result */

   number = gen_rand();

   printf("%d is the power of 2 of %.0lf\n", number, sqrt(number));
}

/* Function generates random number power 2 of 20 - 230 */
int gen_rand()
{
   int n;
   n = rand() % 211;  /* n is random number in range of 0 - 210 */
   n = n + 20; /* n is now in range of 20 - 230 */
   return(n*n); /* return n to the power of 2 */
}
#包括
#包括
#包括/*sqrt()所需*/
#包括/*兰德()所需*/
int gen_rand();/*注意:这些是函数的声明*/
void main()
{
整数;
srand(time(NULL));/*每次运行程序时,都会得到不同的结果*/
编号=gen_rand();
printf(“%d是%.0lf\n”的2的幂,数字,sqrt(数字));
}
/*函数生成20-230的随机数幂2*/
国际货币基金组织
{
int n;
n=rand()%211;/*n是0-210范围内的随机数*/
n=n+20;/*n现在在20-230的范围内*/
返回(n*n);/*返回n到2的幂*/
}

所有非基于硬件的PRNG都需要某种形式的随机输入来对抗其确定性,因此始终需要种子


您可以尝试在linux下使用
/dev/rand
(但它也是一个PRNG),或者如果您有一个非常现代的Intel CPU,它们的新CPU也可以工作

否。如果您不为自动数字生成器设置种子,它的行为将是决定性的,每次都会产生相同的数字。

是和否。 在c语言中,基本上有两种方法可以获得偶数随机数

1) 有一个带种子的伪随机数生成器——这是一个算法,它使用巧妙的算术运算符和可能大量的内部变量(混合、置换、扭曲等等)生成一些数字序列。种子可以是隐式的(即始终为零,并且每次运行程序时,都会生成相同的序列)。或者它可以是显式的,在运行之间种子可以以某种方式改变

2) 使用外部数据源,在运行之间会发生某种变化。这可能来自计时器、环境变量(可能是程序id)、摄像头噪音、鼠标移动等


1+2)使用外部噪声源作为伪随机数生成器的种子。

是。不使用种子而使用
rand()
函数生成随机数将得到相同的随机数集。

我认为,如果不调用
srand()
,种子将始终是相同的。这有什么问题吗?您可以使用rand函数而不设置种子,但它会在每次运行程序时返回相同的“随机”数序列。因此,您想要的是真实的随机数,而不是伪随机数,这是您的问题吗?据我所知,你需要额外的硬件(基本上是盖革计数器)。你能解释一下为什么你需要一个没有种子的随机数生成器吗?在某种意义上,PRNG的内部状态是一个种子,可以在运行之间存储在一个文件中。