How to test the randomness of values from this function?
Moderator: General Moderators
- aditya2071990
- Forum Contributor
- Posts: 106
- Joined: Thu May 22, 2008 11:30 am
- Location: Hyderabad, India
- Contact:
How to test the randomness of values from this function?
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?
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?
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.
- aditya2071990
- Forum Contributor
- Posts: 106
- Joined: Thu May 22, 2008 11:30 am
- Location: Hyderabad, India
- Contact:
Re: How to test the randomness of values from this function?
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?
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.
Best of Luck,
OmniUni
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.
Best of Luck,
OmniUni
- aditya2071990
- Forum Contributor
- Posts: 106
- Joined: Thu May 22, 2008 11:30 am
- Location: Hyderabad, India
- Contact:
Re: How to test the randomness of values from this function?
Wow thanks, I will ask my lecturers about standard deviation...
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Re: How to test the randomness of values from this function?
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?
Ambush, I thought that was depreciated in PHP5?
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Re: How to test the randomness of values from this function?
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?
I misread "mt_srand()" when I was reading the manual the other night.

I think you're suggestion is probably an excellent one now that I read things correctly...
Apologies!
I think you're suggestion is probably an excellent one now that I read things correctly...
Apologies!
- aditya2071990
- Forum Contributor
- Posts: 106
- Joined: Thu May 22, 2008 11:30 am
- Location: Hyderabad, India
- Contact:
Re: How to test the randomness of values from this function?
Thank you, I will definitely try out mt_rand()
Re: How to test the randomness of values from this function?
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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: How to test the randomness of values from this function?
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.

It's just that the chances are slim.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Re: How to test the randomness of values from this function?
Meh, if I could guarantee that the randomly generated numbers would not be the same, I would not have a random number generator.
- aditya2071990
- Forum Contributor
- Posts: 106
- Joined: Thu May 22, 2008 11:30 am
- Location: Hyderabad, India
- Contact:
Re: How to test the randomness of values from this function?
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
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?
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 