




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
在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 );返回的是一个界于032767(0x7FFF)之间的伪随机数,包括0和32767。注意,这里产生的是伪随机数,不是真正意义上的随机数,看下面的程序:#include stdlib.h#include stdio.hvoid main( void )/* Display a number. */printf( %6dn, 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.hvoid main( void )int i;/* Seed the random-number generator with current time so thatthe numbers will be different every time we run.将当前时间设置成随机函数的种子,所以每次产生的数都不一样*/srand( (unsigned)time( NULL ) );/* Display 10 numbers. */for( i = 0; i 10;i+ )printf( “ %6dn”, rand() );Output6929802621987307342058766992203425051798810104每次运行这个程序,产生的随机数都不一样,这样就达到了随机数的要求了。注意,rand这个函数产生的随机数的范围是032767,如果要产生100以内的随机数怎么办呢?在标准C语言库中并没有定义产生给定范围的随机数的函数。其实,要产生给定范围的随机数,只要做一个取余(%)运算就可以了。下面是一个产生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: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 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.*/srand( (unsigned)time( NULL ) );/* Display 10 numbers. */for( i = 0; i 10;i+ )printf( %6dn, random( 10 ) );getchar();另外,在头文件 Stdlib.h 中还可以发现下面的这条语句:#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+ )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( 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 );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( %6dn, rand2( ) );getchar();void srand2( int a )randx = a;int rand2()return (int)( randx * 123265187.7795 + 569412.1256 ) ;每次运行上面的程序,产生的随机数都不一样。总结:1.函数rand()产生的是伪随机数,不是真
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 拆装维修合同范本2016
- 吊装协议合同范本
- 买商铺 合伙 合同范本
- 店面转让合同范本
- 舞蹈创作编排合同范本
- 露营租赁转让合同范本
- 法国住宿证明合同范本
- 水电代缴合同范本
- 投资代理项目合同范本
- 简单进口贸易合同范本
- 初中英语动词过去式不规则变化-过去分词-听写表格
- 博士组合物80问
- 陪玩协议书6篇
- 模块化建筑运输与安装行业跨境出海战略研究报告
- 2025年驾驶证资格考试科目一必刷题库及答案(共560题)
- 青岛科学四年级上册《风的形成》课件
- 2025年光伏发电安装合同模板
- 2025年交规考试宝典
- 家长外出务工委托亲戚照顾孩子全托合同协议书
- 华为SDBE领先模型:闭环战略管理的全面解析-2024-12-组织管理
- 2024版中式烧烤加盟经营合作协议书3篇
评论
0/150
提交评论