Cicero Report post Posted November 12, 2004 I've written some code that can rate the randomness of a random function. Here are the functions I tested and the results so far (lower is better): int my_rand_0(int max) { static unsigned long next =1; next = next * 1103515245 + 12345; return ((unsigned)(next/65536) % 32768)%(max+1); } int my_rand_1(int max) { return rand()%(max+1); } int my_rand_2(int max) { return random()%(max+1); } int my_rand_3(int max) { return (int)((float)max*rand()/(RAND_MAX+1.0)); } Linux: my_rand_0: 1093500000 my_rand_1: 1067800000 my_rand_2: 1073100000 my_rand_3: 1102200000 FreeBSD 4.10 (the server): my_rand_0: 1093500000 my_rand_1: 1079400000 my_rand_2: 1067800000 my_rand_3: 1106200000 FreeBSD (whatever I'm running): my_rand_0: 1093500000 my_rand_1: 1058400000 my_rand_2: 1067800000 my_rand_3: 1084000000 Here's my randomness rating function: long chi_square(int (*f)(int max)) { int gamma[100]; long chi=0; int i; memset(gamma,0,100*sizeof(int)); for(i=0; i<1000; i++) { gamma[f(100)]++; } for(i=0; i<100; i++) { chi += ((gamma * 100)*(gamma * 100))*10; } return chi; } If someone wants to do testing on other platforms and with other random methods, please post results here. BTW, the server is using method 2, which seems to be the best for it. Share this post Link to post Share on other sites
Learner Report post Posted November 12, 2004 You also need to test the randomness with values of 10000, 5000, and 1000 as well since those values are used in some critical areas. I'd also suggest looking at integer equivalents of #4, I'd rather not have to be converting to and from floats that much. One option might be? return (int)((long long)max*(long long)rand()/((long long)RAND_MAX+1)) Share this post Link to post Share on other sites