首页 > 其他 > 详细

生成随机数

时间:2019-11-12 21:13:14      阅读:75      评论:0      收藏:0      [点我收藏+]

简介

随机数生成一般以时间为种子,但是当前时间一般以s为结束,所以要想得到多个基于不同时间的随机数,那么一定要加上sleep

code

#include <iostream>
#include "common.h"
#include <windows.h>
using namespace std;
int main()
{
    for (int i = 0; i < 100; i++) {
        Sleep(1000);
        cout << generateRandom(50, 100) << endl;
    }
    system("pause");
    return 0;
}
-----------------------------------
#include "common.h"
// start 开始的数字
// end   结束的数字  
//       闭区间进行描述
int generateRandom(int start, int end) {
    unsigned seed;  // Random generator seed
        // Use the time function to get a "seed” value for srand
    seed = time(0);
    srand(seed);
    int number = (rand() % (start - end + 1)) + start;
    return number;
}

参考链接

https://blog.csdn.net/hopegrace/article/details/102916787
http://c.biancheng.net/view/1352.html

生成随机数

原文:https://www.cnblogs.com/eat-too-much/p/11844768.html

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