Page 1 of 1

How to test the randomness of values from this function?

Posted: Sun Oct 12, 2008 8:26 am
by aditya2071990
I made a small function that is available in the sixth post of the following thread:

viewtopic.php?f=50&t=89335

How do I test on how random the values are, and whether I can rely on them?

Re: How to test the randomness of values from this function?

Posted: Mon Oct 13, 2008 9:48 am
by Jenk
Test in and tally in a loop that a value has changed, say 10 times. Then look for duplicates in the tally, and perform some sort of assert that there are < 'x' number of duplicates, which means it is acceptable. You'll have to remember that it is a chance thing so you might fail sometimes... this will also potentially be a hideous block of code.

Re: How to test the randomness of values from this function?

Posted: Mon Oct 13, 2008 11:03 am
by aditya2071990
Well I guess I just have to go on with life and believe in the function for all its worth... and I guess I can trust the uniqueness of the values, I mean, its got a reference to microtime AND the user ID, AND a random number. I do guess that I can safely believe in it.

Re: How to test the randomness of values from this function?

Posted: Mon Oct 13, 2008 2:26 pm
by omniuni
So, let's say I want to test the randomness of something that generates 1-10.

I could have it run 10 times, and take the average, it should be somewhere around 5, but it would probably be closer to 4 o4 6.... just because it's random.

Now, say I run it 100 times, the average should be closer to 5.

Find what the average value of your script should be if it is truly random. Run the script 100, 1000, and 5000 times, tally and average the results and check that as you run the script more times, you come closer to your average.

Also, you could just do it 3 times, 20 runs each, and compute a standard deviation, but that takes more math, and I'm too lazy to do more than just mention it as a more mathematical alternative. :wink:

Best of Luck,
OmniUni

Re: How to test the randomness of values from this function?

Posted: Mon Oct 13, 2008 3:07 pm
by aditya2071990
Wow thanks, I will ask my lecturers about standard deviation...

Re: How to test the randomness of values from this function?

Posted: Wed Oct 15, 2008 3:46 pm
by Ambush Commander
So, there's mathematically rigorous ways of testing from randomness (Statistical Randomness may be of interest), but for your case, the randomness of the rand() function is far more suspect; I would recommend using mt_rand() instead.

Re: How to test the randomness of values from this function?

Posted: Wed Oct 15, 2008 3:58 pm
by omniuni
Ambush, I thought that was depreciated in PHP5?

Re: How to test the randomness of values from this function?

Posted: Wed Oct 15, 2008 4:04 pm
by Ambush Commander
Not according to the manual it isn't: http://docs.php.net/manual/en/function.mt-rand.php

Re: How to test the randomness of values from this function?

Posted: Wed Oct 15, 2008 4:58 pm
by omniuni
I misread "mt_srand()" when I was reading the manual the other night.

:oops:

I think you're suggestion is probably an excellent one now that I read things correctly...

Apologies!

Re: How to test the randomness of values from this function?

Posted: Wed Oct 15, 2008 10:52 pm
by aditya2071990
Thank you, I will definitely try out mt_rand()

Re: How to test the randomness of values from this function?

Posted: Thu Oct 16, 2008 10:02 am
by pickle
Unless you record & check against previously generated numbers, there is no way to guarantee that 2 numbers won't be identical. Your randomness set up is pretty good - using microtime() and a random number... but mathematically the possibility exists.

Re: How to test the randomness of values from this function?

Posted: Thu Oct 16, 2008 9:26 pm
by omniuni
Mathematically, Pickle, there is a chance that you are a Cucumis sativus that has developed a brain, and a love of salt water.

It's just that the chances are slim.

:)

Re: How to test the randomness of values from this function?

Posted: Thu Oct 16, 2008 9:27 pm
by Ambush Commander
Meh, if I could guarantee that the randomly generated numbers would not be the same, I would not have a random number generator.

Re: How to test the randomness of values from this function?

Posted: Fri Oct 17, 2008 9:32 am
by aditya2071990
Well, after so much debate, I think it is best to try and simply implement the algorithm I set out with in the first place:

1. Generate a Random Number
2. Check if it exists in the DB
3. If it does, go back to step 1
4. If it don't, use the value and log it in the DB

Re: How to test the randomness of values from this function?

Posted: Sat Oct 18, 2008 2:44 pm
by omniuni
Of course, that only assigns unique values an a random order, it does not produce random numbers, for which some will by necessity be repeated.... You will, if you do that, eventually run out of numbers, unless you plan on assigning numbers like 1284032462093461006923040062650239.234236065190955390001741423894 8O