How to generate random floating point number between 0 and 1 in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main() {
    srand((unsigned int)time(NULL));

    for (int i = 0; i < 25; i++)
        printf("%f\n", (float)rand() / (float)RAND_MAX);
         
    return 0;
}
 
 
/*
run:
 
0.430166
0.561872
0.093331
0.608843
0.272517
0.298066
0.864854
0.731596
0.103419
0.936059
0.634556
0.131789
0.719566
0.396311
0.060300
0.379816
0.690235
0.558314
0.249370
0.870029
0.144419
0.482467
0.460026
0.921403
0.886848
 
*/

 



answered Jan 3, 2021 by avibootz

Related questions

...