Posted By

zava on 08/18/10


Tagged

textmate random generator integer rand 64-bit


Versions (?)

rand64


 / Published in: C
 

  1. #include "stdlib.h"
  2. #include "stdint.h"
  3.  
  4. uint64_t rand64()
  5. {
  6. // Combin 4 parts of low 16-bit of each rand()
  7. uint64_t R0 = (uint64_t)random() << 48;
  8. uint64_t R1 = (uint64_t)random() << 48 >> 16;
  9. uint64_t R2 = (uint64_t)random() << 48 >> 32;
  10. uint64_t R3 = (uint64_t)random() << 48 >> 48;
  11. return R0 | R1 | R2 | R3;
  12. }
  13.  
  14. #define SEGS 100
  15. #define TIMES 50000
  16.  
  17. // Show segments
  18. int main (int argc, char const *argv[])
  19. {
  20. const uint64_t SEG = UINT64_MAX / SEGS;
  21. int f[SEGS] = { 0 };
  22. for(int i = 0; i < TIMES; ++i)
  23. {
  24. uint64_t r = rand64();
  25. f[r / SEG]++;
  26. }
  27. for (int i = 0; i < SEGS; ++i)
  28. {
  29. printf("#%2d: (%20llu ~ %20llu) %dn", i, i * SEG, (i + 1) * SEG - 1, f[i]);
  30. }
  31. return 0;
  32. }

Report this snippet  

You need to login to post a comment.