首页 > 其他 > 详细

随机数生成

时间:2017-07-17 23:24:34      阅读:272      评论:0      收藏:0      [点我收藏+]

遇到了这么个题

1949, 2012, 1946, 1874, 2046, 1994, 1839, 1824, 1999, 1024 
Choose one number from the ten numbers mentioned above. Only one is correct.

正好复习一下生成(伪)随机数的rand和srand...

time(0)获得从某个值得纪念的时刻(1970年1月1日0时)到调用time函数时经过的秒数

srand设置随机数种子,就是说种子相同时产生的“随机”序列也相同,不过每次使用不同的种子(使用time(0)),假装成是真随机就能凑合用了

rand(),得到0-RAND_MAX(在我的系统上是32767)之间的随机数,再取膜你想要的范围就行了

time()需要ctime头文件

srand,rand需要cstdlib

#include<cstdio>
#include<cstdlib>
#include<ctime>
int arr[10] = {1949, 2012, 1946, 1874, 2046, 1994, 1839, 1824, 1999, 1024};
int main()
{
    srand(time(0));
    int num = rand()%10;
    printf("%d", arr[num]);
    return 0;
}

交了四次成功AC,处于(勉强的)欧洲人水平

所以说到最后我也不知道答案是哪个数,interesting

随机数生成

原文:http://www.cnblogs.com/DearDongchen/p/7197744.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!