B1KMusic

RNG Test Cases

Oct 8th, 2016
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. /*
  2. Testing: http://pastebin.com/9D3G0w8R
  3. Where it says "// get_random_number implementation", the implementation is obtained via the above URL
  4. */
  5.  
  6. // Test 1 -- does it have collisions?
  7. // C
  8.     #include <stdio.h>
  9.  
  10.     // get_random_number implementation
  11.  
  12.     int
  13.     main()
  14.     {
  15.         for(int i = 0; i < 65536; i++)
  16.             printf("%i\n", get_random_number(i));
  17.     }
  18.  
  19. /*
  20. Bash
  21.     gcc test.c && ./a.out | vim -
  22.  
  23. Vim
  24.     :%!sort -u
  25.     :file
  26.     :q!
  27.  
  28. Output
  29.     (initial)
  30.     44253
  31.     22970
  32.     ...
  33.     21283
  34.     0
  35.  
  36.     (:%!sort -u)
  37.     0
  38.     1
  39.     10
  40.     100
  41.     ...
  42.     9998
  43.     9999
  44.  
  45.     (:file)
  46.     "[No Name]" [Modified] 65536 lines --100%--
  47. */
  48.  
  49. // Test 2 -- does it get stuck?
  50. // C
  51.     #include <stdio.h>
  52.     #include <time.h>
  53.  
  54.     // get_random_number implementation
  55.  
  56.     int
  57.     main()
  58.     {
  59.         int i = 0;
  60.         int seed = time(0) & 0xFF;
  61.  
  62.         while(seed = get_random_number(seed)) // Terminates when seed = 0
  63.             printf("Generation #%i: %i\n", i++, seed);
  64.     }
  65.  
  66. /*
  67. Bash
  68.     gcc test.c && ./a.out
  69.  
  70. Output
  71.     (1)
  72.     Generation #0: 16088
  73.     Generation #1: 3413
  74.     Generation #2: 19262
  75.     ...
  76.     Generation #36118: 29834
  77.     Generation #36119: 65535
  78.     (loop terminates)
  79.  
  80.     (2)
  81.     gcc test.c && ./a.out
  82.     Generation #0: 32958
  83.     Generation #1: 30947
  84.     Generation #2: 36052
  85.     ...
  86.     Generation #33428: 29834
  87.     Generation #33429: 65535
  88.     (loop terminates)
  89.  
  90.     (3)
  91.     gcc test.c && ./a.out
  92.     Generation #0: 24657
  93.     Generation #1: 16074
  94.     Generation #2: 39231
  95.     ...
  96.     Generation #9793: 29834
  97.     Generation #9794: 65535
  98.     (loop terminates)
  99. */
  100.  
  101. // Test 3 -- can it access all values?
  102. // C
  103.     #include <stdio.h>
  104.     #include <time.h>
  105.  
  106.     // get_random_number implementation
  107.  
  108.     int
  109.     main()
  110.     {
  111.         int orig = time(0) & 0xFF;
  112.         int seed = get_random_number(orig);
  113.  
  114.         for(int i = 0; seed != orig; i++, seed = get_random_number(seed)) // Terminates When seed wraps around to original value
  115.             printf("Generation #%i: %i\n", i, seed);
  116.     }
  117.  
  118. /*
  119. Bash
  120.     gcc test.c && ./a.out
  121.  
  122. Output
  123.     (1)
  124.     Generation #0: 63458
  125.     Generation #1: 33527
  126.     Generation #2: 45080
  127.     ...
  128.     Generation #65533: 45579
  129.     Generation #65534: 50268
  130.     (loop terminates)
  131.     (all other tests end at the same number of generation. It's 65534 because after 50268 is another value that isn't displayed, and after that value is the value before generation #0, also not displayed. 65536 - 2 = 65534)
  132. */
Add Comment
Please, Sign In to add comment