code_junkie

What is the best way to produce random double on POSIX?

Nov 11th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. FILE* f = fopen("/dev/urandom", "r");
  2. int32_t int;
  3. fread(&int, sizeof(int32_t), 1, f);
  4. fclose(f);
  5. double theRandomValue = int / (double) (2 ** 32 - 1);
  6.  
  7. FILE* f = fopen("/dev/urandom", "r");
  8. int32_t int;
  9. fread(&int, sizeof(int32_t), 1, f);
  10. fclose(f);
  11. double theRandomValue = int / (double) (2 ** 32 - 1);
  12.  
  13. #include <stdlib.h>
  14. printf("%fn", drand48());
  15.  
  16. double c;
  17. fd = open("/dev/random", O_RDONLY);
  18. unsigned int a, b;
  19. read(fd, &a, sizeof(a));
  20. read(fd, &b, sizeof(b));
  21. if (a > b)
  22. c = fabs((double)b / (double)a);
  23. else
  24. c = fabs((double)a / (double)b);
Add Comment
Please, Sign In to add comment