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

时间:2023-11-14 10:30:02
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
const int n = ;
/*cstdlib头文件要和ctime一起,否则无法使用srand*/
void RandBetween(int s, int d, int num)
{
int result;
srand((unsigned)time(NULL));
for(int i=; i<num; i++)
{
result = rand() % (d - s) + s + ;// (s,d]
result = rand() % (d - s) + s;// [s,d)
result = rand() % (d - s + ) + s;// [s,d]
cout << result << endl;
}
} int main()
{
int first, second;
cout << "input start and end: ";
cin >> first >> second;
RandBetween(first, second, n);
return ;
}