Guest User

Untitled

a guest
Nov 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. static int do_rand(unsigned long *ctx)
  5. {
  6. /*
  7. * Compute x = (7^5 * x) mod (2^31 - 1)
  8. * wihout overflowing 31 bits:
  9. * (2^31 - 1) = 127773 * (7^5) + 2836
  10. * From "Random number generators: good ones are hard to find",
  11. * Park and Miller, Communications of the ACM, vol. 31, no. 10,
  12. * October 1988, p. 1195.
  13. */
  14. long hi, lo, x;
  15.  
  16. x = *ctx;
  17. /* Can't be initialized with 0, so use another value. */
  18. if (x == 0)
  19. x = 123459876L;
  20. hi = x / 127773L;
  21. lo = x % 127773L;
  22. x = 16807L * lo - 2836L * hi;
  23. if (x < 0)
  24. x += 0x7fffffffL;
  25. return ((*ctx = x) % ((unsigned long)RAND_MAX + 1));
  26. }
  27.  
  28. static unsigned long next = 1;
  29.  
  30. int rand(void)
  31. {
  32. return do_rand(&next);
  33. }
  34.  
  35. void srand(unsigned int seed)
  36. {
  37. next = seed;
  38. }
  39.  
  40.  
  41. int main(int argc, char **argv)
  42. {
  43. char *p;
  44. long conv = strtol(argv[1], &p, 10);
  45. srand(conv); // seed with input
  46. unsigned long start = rand(); // get starting number
  47. if ((start & 0xffff) == 0){
  48. printf("%04x\n", start);
  49. }
  50.  
  51. long unsigned int t;
  52. //for (int i=0; i<8000000; i++) {
  53. while(1){
  54. t = rand();
  55. if (t == start){
  56. printf("We are done!");
  57. return 0;
  58. }
  59. if ((t & 0xffff) == 0){
  60. printf("%04x\n", t);
  61. }
  62. }
  63.  
  64. }
Add Comment
Please, Sign In to add comment