fork download
  1. #include <stdio.h> /* printf() */
  2. #include <stdlib.h> /* rand(), srand(), RAND_MAX */
  3. #include <time.h> /* time() */
  4.  
  5. double randd(double min, double max) {
  6. int r = rand();
  7. double delta = max - min; /* assume max > min */
  8. double tmp = r / (RAND_MAX + 1.0); /* generate up to but excluding max */
  9. return tmp * delta + min;
  10. }
  11.  
  12. int main(void) {
  13. srand(time(0));
  14. printf("%f\n", randd(4.2, 4.3));
  15. printf("%f\n", randd(0.0001, 0.0000001));
  16. printf("%f\n", randd(-0.1, 0.1));
  17. printf("%f\n", randd(2.7182, 3.1415));
  18. return 0;
  19. }
  20.  
Success #stdin #stdout 0s 2112KB
stdin
Standard input is empty
stdout
4.298055
0.000068
0.036887
2.821215