产生随机数字

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 int main(void)
 4 {
 5    int i;
 6    srand((unsigned) time(NULL)); //秒级的种子
 7    printf("Ten random numbers from 0 to 99\n\n");
 8    for(i=0; i<10; i++)
 9       printf("%d\n", rand() % 100);
10    return 0;
11 }

由于rand产生的随机数从0到rand_max,而rand_max是一个很大的数,那么如何产生从X~Y的数呢?
 从X到Y,有Y-X+1个数,所以要产生从X到Y的数,只需要这样写:     k=rand()%(Y-X+1)+X;

//用srand(unsigned int seed)来初始化随机数字
一般用系统的运行时间(毫秒级的)来设置seed
例如在windows下

#include <windows.h>
srand(GetTickCount());
int r=rand();

在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.

所以,要产生真正意义上的随机数,那么就要求每次提供的种子不一样,一

般情况下,都设置时间为随机函数的种子。看下面的一段程序:

 1 /* RAND.C: This program seeds the random-number generator
 2 * with the time, then displays 10 random integers.
 3 */
 4 #include "stdlib.h"
 5 #include "stdio.h"
 6 #include "time.h"
 7 void main( void )
 8 {
 9 int i;
10 /* Seed the random-number generator with current time so that
11 the numbers will be different every time we run.
12 将当前时间设置成随机函数的种子,所以每次产生的数都不一样
13 */
14 srand( (unsigned)time( NULL ) );
15 /* Display 10 numbers. */
16 for( i = 0; i < 10;i++ )
17 printf( “ %6d\n”, rand() );
18 }
19 Output
20 
21 6929
22 8026
23 21987
24 30734
25 20587
26 6699
27 22034
28 25051
29 7988
30 10104

每次运行这个程序,产生的随机数都不一样,这样就达到了随机数的要求了

注意,rand这个函数产生的随机数的范围是0~32767,如果要产生100以内

的随机数怎么办呢?在标准C语言库中并没有定义产生给定范围的随机数的

函数。其实,要产生给定范围的随机数,只要做一个取余(%)运算就可以了

。下面是一个产生10以内随机数的函数:

 1 #include "stdlib.h"
 2 #include "stdio.h"
 3 #include "time.h"
 4 int rand2( void );
 5 void main( void )
 6 {
 7 int i;
 8 /* Seed the random-number generator with current time so that
 9 · the numbers will be different every time we run.
10 */
11 srand( (unsigned)time( NULL ) );
12 /* Display 10 numbers:0~9 */
13 for( i = 0; i < 10;i++ )
14 printf( " %6d\n", rand2() );
15 getchar();
16 }
17 int rand2( void )
18 {
19 return rand() % 10 ;
20 }
21 运行结果:
22 2
23 5 
24 7
25 9
26 0
27 1
28 3
29 5 
30 8
31 3

在这个程序中,我自己写了一个函数rand2(),来产生10以内的随机数,其

实,打开标准库中的头文件 Stdlib.h 就会发现有这样的一条语句:

#define random(num) (rand() % (num))

上面的这行代码是为了方便产生给定范围的随机数的,思路也是采用取余的

方法,所以上面的程序也可以改成:

#include "stdlib.h"
#include "stdio.h"

怎样让c语言中的随机函数真正随机? 

 

 #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))

所以,上面的程序也可以这样写:

 1 #include "stdlib.h"
 2 #include "stdio.h"
 3 #include "time.h"
 4 void main( void )
 5 {
 6 int i;
 7 /* Seed the random-number generator with current time so that
 8 · the numbers will be different every time we run.
 9 */
10 randomize();
11 /* Display 10 numbers. */
12 for( i = 0; i < 10;i++ )
13 printf( " %6d\n", random( 10 ) );
14 getchar();
15 }

下面的一个函数是对随机函数的模拟,设置不同的种子,产生不同的随机数

 

 1 /* 模拟随机数的产生过程
 2 */
 3 #include "stdlib.h"
 4 #include "stdio.h"
 5 #include "time.h"
 6 int randx = 0;
 7 void srand2( int a );
 8 int rand2( void );
 9 void main( void )
10 {
11 int i;
12 int seed;
13 /* 输入不同的种子就可以产生不同的随机数
14 */
15 printf( "Please input a seed: \n");
16 scanf(" %d",&seed);
17 srand2( seed );
18 getchar();
19 printf( " %d\n", rand2( ) );
20 getchar();
21 }
22 void srand2( int a )
23 {
24 randx = a;
25 }
26 int rand2()
27 {
28 return (int)( randx * 123265187.7795 + 569412.1256 ) ;
29 }
30 输入:3 结果:21039
31 输入:9 结果:-27130
32 /* 模拟随机数的产生过程,以时间作为种子
33 */
34 #include "stdlib.h"
35 #include "stdio.h"
36 #include "time.h"
37 int randx = 0;
38 void srand2( int a );
39 int rand2( void );
40 void main( void )
41 {
42 int i;
43 /* Seed the random-number generator with current time so that
44 · the numbers will be different every time we run.
45 */
46 
47 srand2( (unsigned)time( NULL ) );
48 /* Display 10 numbers. */
49 printf( " %6d\n", rand2( ) );
50 getchar();
51 }
52 void srand2( int a )
53 {
54 randx = a;
55 }
56 int rand2()
57 {
58 return (int)( randx * 123265187.7795 + 569412.1256 ) ;
59 }

每次运行上面的程序,产生的随机数都不一样。

总结:
1.函数rand()产生的是伪随机数,不是真正意义上的随机数,这个伪随机数
是根据一个公式算出来的,每次运行程序,产生的伪随机数都一样。
2.要产生真正意义上的随机数,要将函数srand( )和rand()配合使用,函数
srand()用来设置随机数的种子,一般以时间作为种子,当然也有其它
设置种子的方法。
3.设置随机数的种子,可以使用randomize(),它采用时间做为种子。
4.要产生给定范围的随机数,可以使用random()。 

 

 

 

转载自http://blog.sina.com.cn/s/blog_56ee22190100097v.html

 

posted @ 2012-12-02 01:01  _**  阅读(434)  评论(0编辑  收藏  举报