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 between two numbers in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
  
double rand_double() {
    return (double)rand() / (double)RAND_MAX;
}
  
double rand_double_interval(double lower, double upper) {
    return rand_double() * (upper - lower) + lower;
}
  
int main() {
    srand(time(NULL));
      
    for (int i = 0; i < 20; i++) {
        printf("%lf\n", rand_double_interval(7.0, 13.0));
    }
    return 0;
}
  
  
  
/*
run:
  
9.490587
12.246089
8.830585
11.370813
8.041014
12.812351
8.536504
12.999038
12.459223
10.454104
11.576144
7.777342
11.923947
7.426162
12.568848
7.738428
10.933182
11.972028
11.991345
9.085973
  
*/

 



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