|
|
|
问:
C语言中产生随机数的函数是什么?
C语言中产生随机数的函数是什么?(好象有两个)以及这两个函数的头文件是什么?
问题补充:rand和srand有什么区别没?
答:
#include #include #include
void main() { int i;
srand((unsigned)time(NULL)); i = rand() % 100; }
这样产生的随机数就是0-99之间的, 这二个函数的工作过程如下: 1) 首先给srand()提供一个种子,它是一个unsigned
int类型,其取值范围从0~65535; 2) 然后调用rand(),它会根据提供给srand()的种子值返回一个随机数(在0到32767之间)
3) 根据需要多次调用rand(),从而不间断地得到新的随机数; 4)
无论什么时候,都可以给srand()提供一个新的种子,从而进一步“随机化”rand()的输出结果。
------------------------------------------------------------------------------------------------
#include #include
#include int i=0; float n; int n1;
while(i<1) { n=rand(); if(n>0&&n<10.0)
i++; } n1=(int) n;
------------------------------------------------------------------------------------------------
调用rand()函数需要的头文件是
#include
#include #include
void main( void ) { int i;
srand(
(unsigned)time( NULL ) );
for( i = 0; i < 10;i++
) //产生10个随机数 printf( " %6d\n", rand() ); }
------------------------------------------------------------------------------------------------
算法2:线性同余法 1)将种子设为X0, 2)用一个算法X(n+1)=(a*X(n)+b) mod
c产生X(n+1) 一般将c取得很大,可产生0到c-1之间的伪随机数 该算法的一个缺点是会出现循环。 给个C的程序
unsigned int seed = 0; //seed 为种子,要设为全局变量 void mysrand(int i) //初始化种子
{ seed = i; }
int myrand() //产生0--32767之间的随机数
{ seed = (1000*seed+2008) % 32768; return seed; }
调用实例 #include main() { int a;
mysrand(time(0)); a = myrand(); return 0; }
------------------------------------------------------------------------------------------------
下面关于一个另问题的解答:
C语言库函数中的rand()的用法??
悬赏分:0 - 解决时间:2007-1-19 16:46
#include #include
#include
int main(void) { int
i;
printf("Ten random numbers from 0 to 99\n\n"); for(i=0;
i<10; i++) printf("%d\n", rand()%100); getch();
return 0; } 为什么每次运行的结果都是一样的??关了重启结果还是一样!! 这结果哪像是随机啊?!!
求高手帮忙解释一下这个库函数的用法?如果能解释一下上面结果不随机问题最好.谢谢!
答:
函数rand所产生的随机数实际上是伪随机数,即反复调用函数rand所产生的一系列数似乎是随机的,但每次执行程序所产生的序列则是重复的。要产生真正的随机数序列,必须在每一次运行前为rand函数提供不同的种子,这是由srand函数提供的。
所以加上srand(time(NULL))就可以产生真正的随机数了。
#include
#include #include #include
int main(void) { int i;
srand(time(NULL));
printf("Ten random numbers from 0 to
99\n\n"); for(i=0; i<10; i++) printf("%d\n",
rand()%100); getch(); return 0; }
------------------------------------------------------------------------------------------------
在C51下的随机数生成
/* --------------------------------------------
* 描述 : 在 Windows server 2003, vs.net 2003下调试通过
*
* 作者 : e.Studio·zhzkl
* 参考 : rfc4122
* -------------------------------------------*/
#i nclude <stdio.h>
#i nclude <stdlib.h>
#i nclude <conio.h>

#define A 3

int AdvancedLinearCongrutial(int x, int a, int b, int c, int m, int max);


int main(int argc, char* argv[])
  {
char* nums = argv[1]; // 取得参数
char mynums[10]; // 预定义10位长度的字符数组, 单片机为8位 应该不会超过这个范围了
int res = 0, pos = 0, length = 0;

// 将字符流转换为Int
//
while(nums[pos] != '\0' )
 {
length = length + 1;
pos = pos + 1;
}
pos = 0;
length = length - 1;

while(nums[pos] != '\0')
 {
mynums[length - pos] = nums[pos];
pos++;
}

pos = 0;
while(nums[pos] != '\0')
 {
int m = 1;
for(int k = 0; k<pos; k++)
 {
m = m * 10;
}

res = res + (mynums[pos] - 48) * m;
pos = pos + 1;
}

// 定义随机种子
//
srand(res);
int x = rand(), a = 2, b = 3, c = 5, m = 65536;
// 改进的线性同余算法
int i = AdvancedLinearCongrutial(x, a, b, c, 65536, rand()%100);
printf("i = %d", i);
printf("\tres = %d", res);
return i;
}

// 改进的线性同余算法
int AdvancedLinearCongrutial(int x, int a, int b, int c, int m, int max)
  {
for(int i=0; i<max; i++)
 {
a = A;
for(int j=0; j<i; j++)
 {
a *= A;
}
x = (a*x + (a-1)*c/b)%m;
}

return x;
} | | | |