c语言随机函数的使用.doc

上传人:ni****g 文档编号:555326491 上传时间:2023-08-25 格式:DOC 页数:7 大小:32KB
返回 下载 相关 举报
c语言随机函数的使用.doc_第1页
第1页 / 共7页
c语言随机函数的使用.doc_第2页
第2页 / 共7页
c语言随机函数的使用.doc_第3页
第3页 / 共7页
c语言随机函数的使用.doc_第4页
第4页 / 共7页
c语言随机函数的使用.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《c语言随机函数的使用.doc》由会员分享,可在线阅读,更多相关《c语言随机函数的使用.doc(7页珍藏版)》请在金锄头文库上搜索。

1、在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

2、 0x7FFF所以,函数int rand( void );返回的是一个界于032767(0x7FFF)之间的伪随机数,包括0和32767。注意,这里产生的是伪随机数,不是真正意义上的随机数,看下面的程序:#include stdlib.h#include stdio.hvoid main( void )/* Display a number. */printf( %6dn, rand() );getchar();程序运行的结果是:346多次运行这个程序,发现每次产生的结果都是346(不同的机器可能产生的结果不一样),这就是所谓的伪随机数。伪随机数是通过一个公式来运算出来的,所以,每次产生的伪随

3、机数都一样。那么,如何才能产生真正意义上的随机数呢?这就有一个随机种子的问题。在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.所以,要产生真正意义上的随机数,那么就要求每次提供的种子不一样,一般情况下,都设置时间为随机函数的

4、种子。看下面的一段程序:/* 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.hvoid main( void )int i;/* Seed the random-number generator with current time so thatthe numbers will be different every time we run

5、.将当前时间设置成随机函数的种子,所以每次产生的数都不一样*/srand( (unsigned)time( NULL ) );/* Display 10 numbers. */for( i = 0; i 10;i+ )printf( “ %6dn”, rand() );Output6929802621987307342058766992203425051798810104每次运行这个程序,产生的随机数都不一样,这样就达到了随机数的要求了。注意,rand这个函数产生的随机数的范围是032767,如果要产生100以内的随机数怎么办呢?在标准C语言库中并没有定义产生给定范围的随机数的函数。其实,要产

6、生给定范围的随机数,只要做一个取余(%)运算就可以了。下面是一个产生10以内随机数的函数:#include stdlib.h#include stdio.h#include time.hint 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:

7、09 */for( i = 0; i 10;i+ )printf( %6dn, rand2() );getchar();int rand2( void )return rand() % 10 ;运行结果:25 790135 83在这个程序中,我自己写了一个函数rand2(),来产生10以内的随机数,其实,打开标准库中的头文件 Stdlib.h 就会发现有这样的一条语句:#define random(num) (rand() % (num)上面的这行代码是为了方便产生给定范围的随机数的,思路也是采用取余的方法,所以上面的程序也可以改成:#include stdlib.h#include stdi

8、o.h#include time.hvoid 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( %6dn, random( 10 ) );getchar();另外,在头文件 Stdlib.h 中还可以发现下面的这条语句

9、:#define randomize() srand(unsigned)time(NULL)所以,上面的程序也可以这样写:#include stdlib.h#include stdio.h#include time.hvoid 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+

10、 )printf( %6dn, random( 10 ) );getchar();下面的一个函数是对随机函数的模拟,设置不同的种子,产生不同的随机数/* 模拟随机数的产生过程*/#include stdlib.h#include stdio.h#include time.hint 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

11、( seed );getchar();printf( %dn, 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.hint randx = 0;void srand2( int a );int rand2( void );vo

12、id 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( %6dn, 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()。

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号