This question already has an answer here:
- generate random double numbers in c++ 7 answers
- C rand() is not random even when seeded 2 answers
I tried to print rand() double in range [0,1], however the code gives:
0,320261
0,323435
0,327128
0,328837
...
There are random but they not in [0,1] . Shall someone write me a function which will work or is there a similar function in library?
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
double fRand(double fMin, double fMax)
{
return (double)rand()/(RAND_MAX);
}
int main()
{
srand(time(NULL));
cout<<fRand(0,1);
}
Aucun commentaire:
Enregistrer un commentaire