mardi 2 février 2016

Generating a random, uniformly distributed real number in C

I would like to generate a random, real number in the interval [0,1]. I would like to set a pointer, say n, for the number so whenever I stated n, it will be referred to the random generated number.

I have searched on StackOverflow and on Google, but most of them are for C++ or for integers.

I have tried this code suggested to me in the answers:

#include <stdio.h>
#include <stdlib.h>
int main()
{
 double n;
double get_random() { return (double)rand() / (double)RAND_MAX; }
n = get_random();
printf("%f", n);
   return 0;
}

However, I can only get a value 0.00000000.

How could I fix my program?




Aucun commentaire:

Enregistrer un commentaire