Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to generate random floating point number in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main() {
    srand((unsigned int)time(NULL));
 
    float f = 6.0;
     
    for (int i = 0; i < 25; i++)
        printf("%f\n", ( (float)rand() / (float)RAND_MAX ) * f);
         
    return 0;
}
 
 
 
/*
run:
 
4.709688
0.918692
0.570476
0.603086
1.514855
2.832324
3.122437
2.190587
2.580643
3.527491
4.288983
3.537754
5.810797
4.800825
0.635029
1.671287
2.379007
1.131186
4.691870
1.951129
2.391417
4.052518
3.533689
1.412000
3.457078

*/

 



answered Jan 3, 2021 by avibootz
edited Jan 3, 2021 by avibootz
...