Compare distribution of three random integer algorithm from random bytes.
c
sadrand
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sadrand.c 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #include <errno.h>
  2. #include <fcntl.h>
  3. #include <getopt.h>
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/random.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <unistd.h>
  12. #define NTEST 3
  13. struct rand_ctx
  14. {
  15. unsigned char buf[4096];
  16. int cur_read;
  17. };
  18. static struct option long_opts[] = {
  19. {"iter", required_argument, 0, 'i'},
  20. {"resolution", required_argument, 0, 'r'},
  21. {"relative", no_argument, 0, 'R'},
  22. {"help", no_argument, 0, 'h'},
  23. {0, 0, 0, 0}
  24. };
  25. void usage(const char name[])
  26. {
  27. dprintf(2, "Usage %s [-i ITER] [-r RESOLUTION] [-R]\n\
  28. \t-i, --iter Iteration count\n\
  29. \t-r, --resolution Number of dice faces\n\
  30. \t-R, --relative Output relative count instead of absolute\n\
  31. \t-h, --help Display this help and exit\n", name);
  32. }
  33. void rand_ctx_init(struct rand_ctx *ctx)
  34. {
  35. ctx->cur_read = -1;
  36. }
  37. int randbits(char count, unsigned long *bits, struct rand_ctx *ctx)
  38. {
  39. int ret;
  40. *bits = 0;
  41. if(count < 0 || count > 8*sizeof(unsigned long))
  42. {
  43. return -1;
  44. }
  45. if(sizeof(ctx->buf) - ctx->cur_read < (count/8)+1)
  46. {
  47. ctx->cur_read = -1;
  48. }
  49. if(ctx->cur_read < 0)
  50. {
  51. while(1)
  52. {
  53. ret = getrandom(ctx->buf, sizeof(ctx->buf), 0);
  54. if(ret < 0) {
  55. if(errno == EINTR) { continue; }
  56. perror("Unable to get random bytes");
  57. return -2;
  58. }
  59. break;
  60. }
  61. ctx->cur_read = 0;
  62. }
  63. do {
  64. if(count < 8)
  65. {
  66. *bits <<= count;
  67. *bits |= ctx->buf[ctx->cur_read] & ((1<<count)-1);
  68. count = 0;
  69. }
  70. else
  71. {
  72. *bits <<= 8;
  73. *bits |= ctx->buf[ctx->cur_read];
  74. count -= 8;
  75. }
  76. ctx->cur_read++;
  77. }while(count);
  78. return 0;
  79. }
  80. int std_rand(unsigned long iterations, unsigned char resolution, unsigned int *result)
  81. {
  82. unsigned long bits;
  83. struct rand_ctx ctx;
  84. char nbits;
  85. nbits = (char)ceilf(log2f(resolution));
  86. rand_ctx_init(&ctx);
  87. for(unsigned long i = 0; i<iterations; i++)
  88. {
  89. do
  90. {
  91. if(randbits(nbits, &bits, &ctx) < 0) {
  92. perror("Unable to get random bits");
  93. return -1;
  94. }
  95. }while(bits > resolution-1);
  96. result[bits]++;
  97. }
  98. return 0;
  99. }
  100. int mod_rand(unsigned long iterations, unsigned char resolution, unsigned int *result)
  101. {
  102. unsigned long bits;
  103. struct rand_ctx ctx;
  104. rand_ctx_init(&ctx);
  105. for(unsigned long i = 0; i<iterations; i++)
  106. {
  107. if(randbits(8, &bits, &ctx) < 0) {
  108. perror("Unable to get random bits");
  109. return -1;
  110. }
  111. result[bits%resolution]++;
  112. }
  113. return 0;
  114. }
  115. int prop_rand(unsigned long iterations, unsigned char resolution, unsigned int *result)
  116. {
  117. unsigned long bits;
  118. struct rand_ctx ctx;
  119. rand_ctx_init(&ctx);
  120. for(unsigned long i = 0; i<iterations; i++)
  121. {
  122. if(randbits(8, &bits, &ctx) < 0)
  123. {
  124. perror("Unable to get random bits");
  125. return -1;
  126. }
  127. result[(bits * resolution) / 256]++;
  128. }
  129. return 0;
  130. }
  131. void print_result(unsigned char resolution, unsigned int *result)
  132. {
  133. printf("[");
  134. for(int i=0; i<resolution; i++)
  135. {
  136. printf("%d%s", result[i], i<resolution-1?", ":"");
  137. }
  138. printf("]\n");
  139. }
  140. void print_results(unsigned char resolution, unsigned int *results[NTEST])
  141. {
  142. for(int i=0; i<resolution; i++)
  143. {
  144. printf("%d %d ", resolution, i);
  145. for(int j=0; j<NTEST; j++)
  146. {
  147. printf("%d ", results[j][i]);
  148. }
  149. printf("\n");
  150. }
  151. }
  152. void print_results_rel(unsigned char resolution, unsigned long iterations,
  153. unsigned int *results[NTEST])
  154. {
  155. long double res;
  156. for(int i=0; i<resolution; i++)
  157. {
  158. printf("%d %f ", resolution, (double)i / resolution);
  159. for(int j=0; j<NTEST; j++)
  160. {
  161. res = (results[j][i] * resolution);
  162. res /= iterations;
  163. printf("%Lf ", res);
  164. }
  165. printf("\n");
  166. }
  167. }
  168. int main(int argc, char *argv[]) {
  169. unsigned char resolution;
  170. unsigned long iterations;
  171. short relative;
  172. long long tmp_ll;
  173. unsigned int *results[NTEST];
  174. relative = 0;
  175. iterations = 0x1000;
  176. resolution = 100;
  177. while (1)
  178. {
  179. int c;
  180. int option_index = 0;
  181. char *arg_endp;
  182. c = getopt_long(argc, argv, "i:r:Rh",
  183. long_opts, &option_index);
  184. if(c == -1) { break; }
  185. switch(c)
  186. {
  187. case 'i':
  188. tmp_ll = strtoll(optarg, &arg_endp, 0);
  189. if (*arg_endp != '\0' || optarg == arg_endp)
  190. {
  191. dprintf(2, "Unable to parse iteration count '%s' invalid from '%s'\n", optarg, arg_endp);
  192. usage(argv[0]);
  193. exit(1);
  194. }
  195. if(tmp_ll <= 0)
  196. {
  197. dprintf(2, "Invalid iteration count %lld\n",
  198. tmp_ll);
  199. usage(argv[0]);
  200. exit(1);
  201. }
  202. iterations = tmp_ll;
  203. break;
  204. case 'r':
  205. tmp_ll = strtoll(optarg, &arg_endp, 0);
  206. if (*arg_endp != '\0' || optarg == arg_endp)
  207. {
  208. dprintf(2, "Unable to parse resolution '%s' invalid from '%s'\n", optarg, arg_endp);
  209. usage(argv[0]);
  210. exit(1);
  211. }
  212. if(tmp_ll <= 0)
  213. {
  214. dprintf(2, "Invalid resolution %lld\n",
  215. tmp_ll);
  216. usage(argv[0]);
  217. exit(1);
  218. }
  219. else if(tmp_ll > 255)
  220. {
  221. dprintf(2, "Too big resolution %lld (max 255)\n",
  222. tmp_ll);
  223. usage(argv[0]);
  224. exit(1);
  225. }
  226. resolution = tmp_ll;
  227. break;
  228. case 'R':
  229. relative = 1;
  230. break;
  231. case 'h':
  232. usage(argv[0]);
  233. exit(0);
  234. case '?':
  235. break;
  236. }
  237. }
  238. if(optind < argc)
  239. {
  240. dprintf(2, "Invalid arguments : ");
  241. for(int i=optind; i<argc; i++)
  242. {
  243. dprintf(2, "'%s' ", argv[i]);
  244. }
  245. dprintf(2, "\n");
  246. usage(argv[0]);
  247. exit(1);
  248. }
  249. if(relative)
  250. {
  251. iterations = iterations * resolution;
  252. }
  253. for(int i=0; i<NTEST; i++)
  254. {
  255. if( !(results[i] = malloc(sizeof(unsigned int) * resolution)) )
  256. {
  257. perror("Unable to allocate results array");
  258. return 1;
  259. }
  260. bzero(results[i], sizeof(unsigned int)*resolution);
  261. }
  262. std_rand(iterations, resolution, results[0]);
  263. mod_rand(iterations, resolution, results[1]);
  264. prop_rand(iterations, resolution, results[2]);
  265. if(relative)
  266. {
  267. print_results_rel(resolution, iterations, results);
  268. }
  269. else
  270. {
  271. print_results(resolution, results);
  272. }
  273. for(int i=0; i<NTEST; i++)
  274. {
  275. free(results[i]);
  276. }
  277. return 0;
  278. }