首页 > 其他 > 详细

random between [a,b]、(a,b]、[a,b)

时间:2014-04-16 06:04:18      阅读:588      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
const int n = 10;
/*cstdlib头文件要和ctime一起,否则无法使用srand*/
void RandBetween(int s, int d, int num)
{
    int result;
    srand((unsigned)time(NULL));
    for(int i=0; i<num; i++)
    {
        result = rand() % (d - s) + s + 1;// (s,d]
        result = rand() % (d - s) + s;// [s,d)
        result = rand() % (d - s + 1) + s;// [s,d]
        cout << result << endl;
    }
}

int main()
{
    int first, second;
    cout << "input start and end: ";
    cin >> first >> second;
    RandBetween(first, second, n);
    return 0;
}
bubuko.com,布布扣

 

random between [a,b]、(a,b]、[a,b),布布扣,bubuko.com

random between [a,b]、(a,b]、[a,b)

原文:http://www.cnblogs.com/buptmuye/p/3667313.html

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