Jump to content
Eternal Lands Official Forums
Sign in to follow this  
Cicero

Randomness

Recommended Posts

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×