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,872 questions

51,796 answers

573 users

How to generate a random double number between min and max in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
double GenerateRandomDoubleNumberMinMax(double min, double max) {
    double range = max - min + 1;
     
    return ((double)rand() / RAND_MAX) * range + min;
}
 
int main() {
    srand(time(NULL)); // Seed the random number generator
    double min = 70.0;
    double max = 100.0;
 
    double d = GenerateRandomDoubleNumberMinMax(min, max);
     
    printf("%f\n", d);
     
    return 0;
}

     
/*
run:
  
86.330671
  
*/

 



answered Jul 21, 2024 by avibootz
edited Apr 19, 2025 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

double getRandomDouble(double min, double max) {
    return min + drand48() * (max - min);
}

int main() {
    srand48(time(NULL)); // Seed the random number generator
    
    double randomDouble = getRandomDouble(20, 35);
    
    printf("Random double: %f\n", randomDouble);
    return 0;
}

   
   
/*
run:
   
Random double: 30.819647
3.588470
   
*/

 



answered Apr 19, 2025 by avibootz
...