R
Roman Töngi
I want to get a random number between 0 and 1.
The following code works but it seems to me a litte
awkward. Is there a "better" solution.
double rnd;
int integerRnd;
srand(static_cast<unsigned>(time(NULL)));
for (int i = 0; i < 9; ++i) {
// rand() returns a value from 0 to 32767
rnd = 10000. / rand();
// integer part of quotient
integerRnd = rnd;
// every random number in the form of 0.xxx
rnd -= integerRnd;
cout << rnd << endl;
}
The following code works but it seems to me a litte
awkward. Is there a "better" solution.
double rnd;
int integerRnd;
srand(static_cast<unsigned>(time(NULL)));
for (int i = 0; i < 9; ++i) {
// rand() returns a value from 0 to 32767
rnd = 10000. / rand();
// integer part of quotient
integerRnd = rnd;
// every random number in the form of 0.xxx
rnd -= integerRnd;
cout << rnd << endl;
}