PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Nov 23, 2014 9:19 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 15 posts ] 
Author Message
PostPosted: Sun Oct 12, 2008 8:26 am 
Offline
Forum Contributor
User avatar

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


Top
 Profile  
 
PostPosted: Mon Oct 13, 2008 9:48 am 
Offline
DevNet Master
User avatar

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


Top
 Profile  
 
PostPosted: Mon Oct 13, 2008 11:03 am 
Offline
Forum Contributor
User avatar

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


Top
 Profile  
 
PostPosted: Mon Oct 13, 2008 2:26 pm 
Offline
Forum Regular
User avatar

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


Top
 Profile  
 
PostPosted: Mon Oct 13, 2008 3:07 pm 
Offline
Forum Contributor
User avatar

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


Top
 Profile  
 
PostPosted: Wed Oct 15, 2008 3:46 pm 
Offline
DevNet Master
User avatar

Joined: Mon Oct 25, 2004 9:29 pm
Posts: 3698
Location: New Jersey, US
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.


Top
 Profile  
 
PostPosted: Wed Oct 15, 2008 3:58 pm 
Offline
Forum Regular
User avatar

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


Top
 Profile  
 
PostPosted: Wed Oct 15, 2008 4:04 pm 
Offline
DevNet Master
User avatar

Joined: Mon Oct 25, 2004 9:29 pm
Posts: 3698
Location: New Jersey, US
Not according to the manual it isn't: http://docs.php.net/manual/en/function.mt-rand.php


Top
 Profile  
 
PostPosted: Wed Oct 15, 2008 4:58 pm 
Offline
Forum Regular
User avatar

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


Top
 Profile  
 
PostPosted: Wed Oct 15, 2008 10:52 pm 
Offline
Forum Contributor
User avatar

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


Top
 Profile  
 
PostPosted: Thu Oct 16, 2008 10:02 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6433
Location: 53.01N x 112.48W
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.


Top
 Profile  
 
PostPosted: Thu Oct 16, 2008 9:26 pm 
Offline
Forum Regular
User avatar

Joined: Tue Jul 15, 2008 10:50 pm
Posts: 738
Location: Carolina, USA
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.

:)


Top
 Profile  
 
PostPosted: Thu Oct 16, 2008 9:27 pm 
Offline
DevNet Master
User avatar

Joined: Mon Oct 25, 2004 9:29 pm
Posts: 3698
Location: New Jersey, US
Meh, if I could guarantee that the randomly generated numbers would not be the same, I would not have a random number generator.


Top
 Profile  
 
PostPosted: Fri Oct 17, 2008 9:32 am 
Offline
Forum Contributor
User avatar

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


Top
 Profile  
 
PostPosted: Sat Oct 18, 2008 2:44 pm 
Offline
Forum Regular
User avatar

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group