文档库 最新最全的文档下载
当前位置:文档库 › c语言rand的用法

c语言rand的用法

c语言rand()随机数的用法2008-05-10 23:11srand()就是给rand()提供种子seed

如果srand每次输入的数值是一样的,那么每次运行产生的随机数也是一样的,

srand(n)

for(10)

rand()
也就是说,以一个固定的数值作为种子是一个缺点。 通常的做法是 以这样一句代码srand((unsigned) time(NULL));来取代,这样将使得种子为一个不固定的数, 这样产生的随机数就不会每次执行都一样了。


1,先看一个例子
#include
#include
#include
using namespace std;
int main( void )
{
int i;
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
}

2.关于time.h
time.h中包含很多有趣的函数,譬如
char *ctime(long *clock)
本函数把clock所指的时间(如由函数time返回的时间)转换成下列格式的
字符串:Mon Nov 21 11:31:54 1983\n\0

#i nclude
#i nclude
#i nclude
using namespace std;


void main()
{
time_t t1,t2;
char getTime[20];
char *ptstring=getTime;

int x,count=0;
x=RAND_MAX;
cout<<<'/n';
t1=time(NULL);
ptstring=ctime(&t1);

while(count<=100)
{
srand( (unsigned)time( NULL ) );
x=rand()%50;
if(x<5)
continue;
else
{
count++;
cout<<"the numth is "<<<'\n';
}
}

查看ptstring的值会显示 "Tue Sep 13 16:31:06 2005"

3, 最后说说srand()函数
void srand(unsigned seed) 初始化随机数发生器

有讨论如下:
1.C的函数库之所以没有把使用系统时钟初始化随机种子这步重要的操作直接放进ran

d函数的实现中,我觉得至少有三个原因:
(1)可以高效产生连续的随机数,不用每次都初始化;
(2)给程序员以更高的灵活性,因为可能在要求较高的场合,应该使用更好的的数据

做种子,而不是系统时钟;
(3)对于只是想产生大量伪随机数来尽兴某种验证或者统计,未必需要初始化,大不

了程序每次运行都产生同样的一系列随机数而已——有些情况下,这是无所谓的。

事实上有一个更重要的原因:
作为伪随机序列产生器的rand()函数,必须具备的一个重要特性就是-》产生的序

列必须是可重现的。
这不仅仅是一个算法,相当大的程度上,它关系到代码测试的准确性。如果算法中

使用了和rand()的结果相关的数据,通过一个可控的可重现序列,我们就有机会再现每一

次测试的过程,从而更有效的找到问题的所在。
所以这里提出一个建议,代码中,如果rand()的函数结果关系到算法的结果,那么

,必须保证你的rand()调用是可重现的。

4,c语言里

函数rand()和srand()的用法 - -

rand(void)用于产生一个伪随机unsigned int 整数。
srand(seed)用于给rand()函数设定种子。

srand 和 rand 应该组和使用。一般来说,srand 是对 rand 进行设置。
比如:
srand((UINT)GetCurrentTime());
int x = rand() % 100;
是生成 0 到 100 之间的随机数。

srand()是用来初始化随机种子数的,因为rand的内部实现是用线性同余法做的,他不是真

的随机数,只不过是因为其周期特别长,所以有一定的范围里可看成是随机的,式子如下


rand = rand*const_1 + c_var;
srand函数就是给它的第一个rand值。

用"int x = rand() % 100;"来生成 0 到 100 之间的随机数这种方法是不或取的,
比较好的做法是: j=(int)(n*rand()/(RAND_MAX+1.0)) 产生一个0到n之间的随机


RAND_MAX=0x7fffffff

5.总结
1)srand()给rand()提供种子
2)srand()中的seed一般由时间函数得,eg srand((UINT)GetCurrentTime()) srand( (u

nsigned)time( NULL ) ) time()函数得到现在的系统时间...等等

//////////
例子1:
#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);
return 0;
}
====================================
例子2:
以下是MSDN的一个例子:

// crt_rand.c
/* This program seeds the random-number generator
* with the time, then displays 10 random integers.
*/

#include
#include
#include

int main( void )
{
int i;

/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );

/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
}
==============================


例子1:
产生的随机数每次都是一样的,在单个程序里运行,如:for循环,每次循环的值还是不一样.
只是再运行这个程序的话,和前一次一样.
例子2:
产生的随机数是不一样的.为什么呢?
那里不是有英文帮助吗?
srand( (unsigned)time( NULL ) );
就是给随机数产生一个seed,我们就叫他种子吧.
time的值每时每刻都不同.
所以种子不同,所以...产生的随机数也不同..
就这个道理.
在C语言里调用rand之前,最好用一个srand(int x);
that's all





for (i=0;i
a[i]=i+1;

for (i=0;i
{w=rand()%(n-i)+i;

t=a[i];

a[i]=a[w];

a[w]=t;

}

#include
#include
#include
#include

const int AX_VAL = 32767;

double U_Rand(double a, double b) // 均匀分布
{
double x = rand();
return a + (b - a) * (x + 1) / (MAX_VAL + 1);
}

double P_Rand(double Lamda)

// 泊松分布
{
double x = -1, u;
double log1, log2;

log1 = 0;
log2 = -Lamda;

do
{
u = U_Rand(0, 1);
log1 += log(u);
x++;
}while(log1 >= log2);

return x;

}

void main()
{
int i;
srand(time(NULL));
for(i=0;i<10000;i++)
printf("%lf/n",P_Rand(100000));

}

相关文档