How to test the randomness of values from this function?

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

How to test the randomness of values from this function?

Postby aditya2071990 » Sun Oct 12, 2008 8:26 am

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?
User avatar
aditya2071990
Forum Contributor
 
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India

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

Postby Jenk » Mon Oct 13, 2008 9:48 am

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.
User avatar
Jenk
DevNet Master
 
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

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

Postby aditya2071990 » Mon Oct 13, 2008 11:03 am

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.
User avatar
aditya2071990
Forum Contributor
 
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India

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

Postby omniuni » Mon Oct 13, 2008 2:26 pm

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
User avatar
omniuni
Forum Regular
 
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

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

Postby aditya2071990 » Mon Oct 13, 2008 3:07 pm

Wow thanks, I will ask my lecturers about standard deviation...
User avatar
aditya2071990
Forum Contributor
 
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India

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

Postby Ambush Commander » Wed Oct 15, 2008 3:46 pm

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.
User avatar
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?

Postby omniuni » Wed Oct 15, 2008 3:58 pm

Ambush, I thought that was depreciated in PHP5?
User avatar
omniuni
Forum Regular
 
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

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

Postby Ambush Commander » Wed Oct 15, 2008 4:04 pm

Not according to the manual it isn't: http://docs.php.net/manual/en/function.mt-rand.php
User avatar
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?

Postby omniuni » Wed Oct 15, 2008 4:58 pm

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!
User avatar
omniuni
Forum Regular
 
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

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

Postby aditya2071990 » Wed Oct 15, 2008 10:52 pm

Thank you, I will definitely try out mt_rand()
User avatar
aditya2071990
Forum Contributor
 
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India

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

Postby pickle » Thu Oct 16, 2008 10:02 am

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.
User avatar
pickle
Briney Mod
 
Posts: 6431
Joined: Mon Jan 19, 2004 7:11 pm
Location: 53.01N x 112.48W

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

Postby omniuni » Thu Oct 16, 2008 9:26 pm

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.

:)
User avatar
omniuni
Forum Regular
 
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

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

Postby Ambush Commander » Thu Oct 16, 2008 9:27 pm

Meh, if I could guarantee that the randomly generated numbers would not be the same, I would not have a random number generator.
User avatar
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?

Postby aditya2071990 » Fri Oct 17, 2008 9:32 am

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
User avatar
aditya2071990
Forum Contributor
 
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India

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

Postby omniuni » Sat Oct 18, 2008 2:44 pm

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
User avatar
omniuni
Forum Regular
 
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA


Return to Testing

Who is online

Users browsing this forum: No registered users and 3 guests