我需要随机产生两个数字L和R,使得1≤L≤R≤10^ 18。 怎么做到这一点?请帮忙。
我的代码:
#define MAXN 1000
int main(){
srand(time(NULL));
int x=rand()%MAXN;
int y=rand()%100000;
int z=rand()%100000;
long L =x;
long R=x*y*z;
assert(L<=R && L>=1 && L<=1000000000000000000 && R>=1 && R<=1000000000000000000);
cout<<L<<" "<<R<<"\n";
}
显然它不正确。所以我正在寻找最好的方法。
方法2:
const long min = 1;
const long max = 1000000000000000000;
std::default_random_engine generator(rd());
std::uniform_int_distribution<long> distribution(min,max);
long L = distribution(generator);
const long min1 = L;
const long max1 = 1000000000000000000;
std::default_random_engine generator1(rd());
std::uniform_int_distribution<long> distribution1(min1,max1);
long R = distribution1(generator1);
assert(L<=R && L>=1 && L<=1000000000000000000 && R>=1 && R<=1000000000000000000);
cout<<L<<" "<<R<<"\n";