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

#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;
}
原文地址:https://www.cnblogs.com/buptmuye/p/3667313.html