在C语言函数库中包含了一个产生随机数的函数:
int rand( void );
在函数库中对这个函数的说明是:
The rand function returns a pseudorandom integer in the range
0 to RAND_MAX. Use the srand function to seed the pseudorandom
-number generator before calling rand.
而在C语言函数库中是这样定义RAND_MAX的:
/* Maximum value returned by "rand" function
*/
#define RAND_MAX 0x7FFF
所以,函数int rand( void );返回的是一个界于0~32767(0x7FFF)之
间的伪随机数,包括0和32767。注意,这里产生的是伪随机数,不是真正意
义上的随机数,看下面的程序:
#include "stdlib.h"
#include "stdio.h"
void main( void )
{
/* Display a number. */
printf( " %6d/n", rand() );
getchar();
}
程序运行的结果是:
346
多次运行这个程序,发现每次产生的结果都是346(不同的机器可能产生
的结果不一样),这就是所谓的伪随机数。伪随机数是通过一个公式来运算
出来的,所以,每次产生的伪随机数都一样。那么,如何才能产生真正意义
上的随机数呢?这就有一个随机种子的问题。在C语言标准函数库中,有这
么一个函数:
void srand( unsigned int seed );
在《The c programming language》中对这个函数是这样描述的:
srand uses seed(函数变量声明中的seed) as the seed(随机函数中种子
的意思) for a new sequence of pseudo-random numbers. The
initial seed is 1.
所以,要产生真正意义上的随机数,那么就要求每次提供的种子不一样,一
般情况下,都设置时间为随机函数的种子。看下面的一段程序:
/* RAND.C: This program seeds the random-number generator
* with the time, then displays 10 random integers.
*/
#include "stdlib.h"
#include "stdio.h"
#include "time.h"
void 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() );
}
Output
6929
8026
21987
30734
20587
6699
22034
25051
7988
10104
每次运行这个程序,产生的随机数都不一样,这样就达到了随机数的要求了
。
注意,rand这个函数产生的随机数的范围是0~32767,如果要产生100以内
的随机数怎么办呢?在标准C语言库中并没有定义产生给定范围的随机数的
函数。其实,要产生给定范围的随机数,只要做一个取余(%)运算就可以了
。下面是一个产生10以内随机数的函数:
#include "stdlib.h"
#include "stdio.h"
#include "time.h"
int rand2( void );
void 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:0~9 */
for( i = 0; i < 10;i++ )
printf( " %6d/n", rand2() );
getchar();
}
int rand2( void )
{
return rand() % 10 ;
}
运行结果:
2
5
7
9
0
1
3
5
8
3
在这个程序中,我自己写了一个函数rand2(),来产生10以内的随机数,其
实,打开标准库中的头文件 Stdlib.h 就会发现有这样的一条语句:
#define random(num) (rand() % (num))
上面的这行代码是为了方便产生给定范围的随机数的,思路也是采用取余的
方法,所以上面的程序也可以改成:
#include "stdlib.h"
#include "stdio.h"
#include "time.h"
void 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", random( 10 ) );
getchar();
}
另外,在头文件 Stdlib.h 中还可以发现下面的这条语句:
#define randomize() srand((unsigned)time(NULL))
所以,上面的程序也可以这样写:
#include "stdlib.h"
#include "stdio.h"
#include "time.h"
void main( void )
{
int i;
/* Seed the random-number generator with current time so that
· the numbers will be different every time we run.
*/
randomize();
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d/n", random( 10 ) );
getchar();
}
下面的一个函数是对随机函数的模拟,设置不同的种子,产生不同的随机数
/* 模拟随机数的产生过程
*/
#include "stdlib.h"
#include "stdio.h"
#include "time.h"
int randx = 0;
void srand2( int a );
int rand2( void );
void main( void )
{
int i;
int seed;
/* 输入不同的种子就可以产生不同的随机数
*/
printf( "Please input a seed: /n");
scanf(" %d",&seed);
srand2( seed );
getchar();
printf( " %d/n", rand2( ) );
getchar();
}
void srand2( int a )
{
randx = a;
}
int rand2()
{
return (int)( randx * 123265187.7795 + 569412.1256 ) ;
}
输入:3 结果:21039
输入:9 结果:-27130
/* 模拟随机数的产生过程,以时间作为种子
*/
#include "stdlib.h"
#include "stdio.h"
#include "time.h"
int randx = 0;
void srand2( int a );
int rand2( void );
void main( void )
{
int i;
/* Seed the random-number generator with current time so that
· the numbers will be different every time we run.
*/
srand2( (unsigned)time( NULL ) );
/* Display 10 numbers. */
printf( " %6d/n", rand2( ) );
getchar();
}
void srand2( int a )
{
randx = a;
}
int rand2()
{
return (int)( randx * 123265187.7795 + 569412.1256 ) ;
}
每次运行上面的程序,产生的随机数都不一样。
总结:
1.函数rand()产生的是伪随机数,不是真正意义上的随机数,这个伪随机数
是根据一个公式算出来的,每次运行程序,产生的伪随机数都一样。
2.要产生真正意义上的随机数,要将函数srand( )和rand()配合使用,函数
srand()用来设置随机数的种子,一般以时间作为种子,当然也有其它
设置种子的方法。
3.设置随机数的种子,可以使用randomize(),它采用时间做为种子。
4.要产生给定范围的随机数,可以使用random()。
由上自己写的程序:
SCR_VOID CW_GEN( )
{
int i;
srand( (unsigned)time( NULL ) );
int seed=rand();
srand(seed);
for( i = 0; i < 16;i++ )
{
buffer[i]=(char)(rand()%256);
}
printf( " 0x%02x/n", buffer[0]);
buffer[16]='/0';
}
作为一个函数模块被其他函数调用