mardi 2 mai 2017

Random double in range [0,1] C++ [duplicate]

This question already has an answer here:

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