C_C++随机数据生成(is how to use but not how it work)
来源:互联网 发布:网络男歌手的伤感歌曲 编辑:程序博客网 时间:2024/06/06 01:14
1.int rand(void)
库:stdlib.h
功能:随机数发生器,值在0至RAND_MAX之间,RAND_MAX定义在stdlib.h, 其值为2147483647
解释:首先不是真的随机数,只是周期比较长。然后如果不设置随机数种子默认是1.
2.void srand(unsigned int seed)
库:stdlib.h
功能:初始化随机数种子
解释:初始化种子,要不每次重新运行程序随机数是一样的。但是重新开始的时候种子仍然是原先设定,所以一般会和time()一起使用。
3.time_t time(time_t* t);
库:time.h
功能:取得从1970年1月1日至 t 的秒数
解释:time_t是long数据类型,前面用typedef重命名的。如果参数是空,默认是当前时间。
下面看具体使用:
1.最简单的生成随机数
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <time.h>using namespace std;int main(){ for(int i=0;i <10;++ i) printf("%d ",rand()); return 0;}
2.结合time设置随机数种子
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <time.h>using namespace std;int main(){ srand((unsigned int)time(NULL)); for(int i=0;i <10;++ i) printf("%d ",rand()); return 0;}
3.设定指定区域的随机数
思路:如果想得到[a,b]的随机数,等价于获得一个随机数n
,将这个随机数和a相加,可以保证
推广一下:
[a,b] a+rand()%(b-a+1)
(a,b] a+1+rand()%(b-a+1)
[a,b) a+rand()%(b-a)
(a,b) a+1+rand(b-a)
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <time.h>using namespace std;int main(){ srand((unsigned int)time(NULL)); int a = 0,b = 0; printf("input a ans b:\n"); scanf("%d%d",&a,&b); printf("get random number which range is [%d,%d]:",a,b); for(int i=0; i <10; ++ i) printf("%d ",a+rand()%(b-a+1)); return 0;}
4.推广一下负数
首先设想一下负数,自然想到上述范围公式,如果将负数代入会怎样?例如我们想要得到[-7,5]之间的随机数,结果如图:
其实上面我们已经证明了范围取值的正确性,负数显然是整数也是符合上述的规则的。
很容易想到
对于上面的范围,直接想到RAND_MAX/2减去产生的随机数。但是现实很残酷,竟然没有超过一半的!!!不知道原因,这里主要是为了利用上述的思想,我还是想到了一个办法,用一个随机数-随机数。(或者利用计算机存储数值的特点进行二进制左移操作也能得到负数,但是这是简单问题复杂化了)
5.推广一下小数
很容易用分治的思想将产生一个小数分解成产生一个随机整数和[0,1)的小数问题。产生指定整数上面讨论过了,只要知道了[0,1)的随机小数怎么产生就行了。
我们知道rand()产生的是[0,RAND_MAX]的整数,那么我们直接将这个数除以RAND_MAX不就得到了所需的随机小数!
分治完成,归并就可以推广到指定小数了。
- C_C++随机数据生成(is how to use but not how it work)
- "Firefox is already running but is not responding" error message - How to fix it
- It is going well-Scene to how work is going
- Introduction to COM - What It Is and How to Use It.(ZT)
- Ubuntu 16.04: How to resolve libqt5x11extras5 (>= 5.1.0) but it is not going to be installed
- What is JNI Graphics and how to use it?
- What is reflection and how to use it in CSharp ?
- AutoResetEvent.How to use it
- how to solve sourceinsight can not work
- how-to: resolve crontab does not work
- Introduction to COM - What It Is and How to Use It.
- Introduction to COM - What It Is and How to Use It.
- Introduction to COM - What It Is and How to Use It.
- MySpace Unraveled: What it is and how to use it safely
- Lock,Moniter,Mutex.How to use it
- How to Best Use Collaboration IT
- WHAT IS ISO? A CAMERA’S SENSITIVITY TO LIGHT EXPLAINED, AND HOW TO USE IT
- iOS开发KVC——what is about kvc and how to use it ? (总结更新版)
- Shell脚本编程-反引号,单引号,双引号
- UVA 10733 - The Colored Cubes【Polya定理】
- IP的种类与获取方式
- HDU 1019 Least Common Multiple
- Logistic Regression 模型简介
- C_C++随机数据生成(is how to use but not how it work)
- HDU 1005 Number Sequence 寻找周期
- 简单的全局404,500异常处理
- Mysql 分页
- MyEclipse代码折叠/展开快捷键设置
- github设置添加SSH
- 哈夫曼树,哈夫曼编码
- 打造Win7下漂亮的vc++6.0
- Unity多个场景叠加或大场景处理方法小结