logo

Random floating point numbers in C

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> double rand_double() { return ((double)rand()) / ((double)RAND_MAX; } double rand_double_interval(double a, double b) { return rand_double() * (b - a) + a; } int main(int argc, char* argv[]) { srand(time(NULL)); for (int i = 0; i < 100; i++) { printf("%lf ", rand_double_interval(5, 10)); } return 0; }
Course thumb

The C programming language made simple