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

51,694 answers

573 users

How to generate random floating point numbers in C

4 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
  
#define N 10
 
void print_array(double arr[], int len);
double rand_double(double min, double max);
 
int compare(const void *a, const void *b);
  
int main(void)
{
    int i;
    double arr[N] = { 0 };    
         
    srand(time(NULL));
    for (i = 0; i < N; i++)
         arr[i] = rand_double(0, 1); // between 0.00 and 1.00
    print_array(arr, N);
    printf("\n");
     
    srand(time(NULL));
    for (i = 0; i < N; i++)
         arr[i] = rand_double(1, 10);
    print_array(arr, N);
    printf("\n");
     
    return 0;
}
  
double rand_double(double min, double max) {
    double range = (max - min); 
    double div = RAND_MAX / range;

    return min + (rand() / div);
}
 
void print_array(double arr[], int len) {
    int i;
      
    for (i = 0; i < len; i++) {
        printf("%6.2f", arr[i]);
    }
}
  

 
/* 
run:
 
  0.69  0.78  0.45  1.00  0.67  0.87  0.73  0.26  0.93  0.64
  7.23  8.03  5.05  9.97  7.04  8.82  7.53  3.31  9.37  6.77
 
*/



answered Sep 27, 2014 by avibootz
edited Jan 10 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

float frand(void) {
    return (float)rand() / (float)RAND_MAX;
}

float frand_range(float min, float max) {
    return min + (max - min) * frand();
}

int main(void) {
    // Seed the PRNG with current time
    srand((unsigned int)time(NULL));

    printf("Random floats in [0,1):\n");
    for (int i = 0; i < 5; i++) {
        printf("  %f\n", frand());
    }

    printf("\nRandom floats in [5.0, 10.0):\n");
    for (int i = 0; i < 5; i++) {
        printf("  %f\n", frand_range(5.0f, 10.0f));
    }

    return 0;
}

 
/* 
run:
 
Random floats in [0,1):
  0.168174
  0.569720
  0.455470
  0.655931
  0.645302

Random floats in [5.0, 10.0):
  9.687235
  8.017874
  8.015210
  9.345604
  5.077385
 
*/

 



answered Jan 10 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

double drand_c11(unsigned int *state) {
    return rand_r(state) / (double)RAND_MAX;
}

int main(void) {
    unsigned int state = (unsigned int)time(NULL);  // seed the local state

    printf("Random floats in [5.0, 10.0):\n");
    for (int i = 0; i < 5; i++) {
        double r = drand_c11(&state);               // pass pointer to state
        double scaled = 5.0 + r * (10.0 - 5.0);     // scale to [5,10)
        printf("  %lf\n", scaled);
    }

    return 0;
}

 
/* 
run:
 
Random floats in [5.0, 10.0):
  8.008771
  8.768305
  7.319217
  7.165096
  5.450201
 
*/

 



answered Jan 10 by avibootz
0 votes
#include <stdio.h>
#include <stdint.h>
#include <sys/random.h>

double secure_drand(void) {
    uint32_t x;
    getrandom(&x, sizeof(x), 0);
    
    return x / (double)UINT32_MAX;   // value in [0,1]
}

double secure_drand_range(double min, double max) {
    return min + (max - min) * secure_drand();
}

int main(void) {
    printf("Random floats in [5.0, 10.0):\n");
    for (int i = 0; i < 5; i++) {
        printf("  %lf\n", secure_drand_range(5.0, 10.0));
    }

    return 0;
}


 
/* 
run:
 
Random floats in [5.0, 10.0):
  8.418336
  5.403445
  9.497760
  9.010841
  6.733069
 
*/

 



answered Jan 10 by avibootz
...