Page 1 of 2

Generating random unique numbers

Posted: Sun Mar 27, 2005 10:55 pm
by dard
I have been pulling my hair out trying to generate code to generate a series of random unique numbers.

I tried using the 'shuffle' function in PHP - here is the code from the php.net site:

Code: Select all

$numbers = range(1, 20);
srand((float)microtime() * 1000000);
shuffle($numbers);
while (list(, $number) = each($numbers)) {
    echo "$number ";
}
I received errors with this code. Then I tried another nice little piece of code found in another forum:

Code: Select all

$arr = array(); 
while ( count($arr) < 10 ) { 
    $x = mt_rand(1,10); 
    if ( !in_array($x,$arr) ) { $arr[] = $x; echo "$x<br>"; } 
}
I'd prefer to use the second method because it is truly random, but am stumped as to where the error appears. Can anyone please help?

Thanks!

Re: Generating random unique numbers

Posted: Sun Mar 27, 2005 11:18 pm
by Roja
dard wrote:I have been pulling my hair out trying to generate code to generate a series of random unique numbers.

I tried using the 'shuffle' function in PHP - here is the code from the php.net site:

Code: Select all

$numbers = range(1, 20);
srand((float)microtime() * 1000000);
shuffle($numbers);
while (list(, $number) = each($numbers)) {
    echo "$number ";
}
I received errors with this code.
I ran the code above, and didnt receive any errors - what errors did you get?
dard wrote: Then I tried another nice little piece of code found in another forum:

Code: Select all

$arr = array(); 
while ( count($arr) < 10 ) { 
    $x = mt_rand(1,10); 
    if ( !in_array($x,$arr) ) { $arr[] = $x; echo "$x<br>"; } 
}
I'd prefer to use the second method because it is truly random, but am stumped as to where the error appears. Can anyone please help?

Thanks!
I get no errors in the second method either - and if the errors are in the first method, why would that stop you from using the second one?

Finally, neither method is "Truly random" - mt_rand is more reliably random (than srand/rand), but it is still a psuedo-random generator. "Truly random" is a very high standard that the vast majority of coders don't actually need. :)

Posted: Mon Mar 28, 2005 7:48 am
by dard
Here are links to each with their respective error messages:

1) http://www.dzgdesigns.com/randomnumber.php (this is the first example)

2) http://www.dzgdesigns.com/randomnumber2.php (this is the second example)

I tried the second one when I received the error on the first and got stuck figuring out why!

Thanks!

Posted: Mon Mar 28, 2005 8:01 am
by Chris Corbyn
Did you do a full copy & paste? Seems like you have a parens missing or something :?

Posted: Mon Mar 28, 2005 11:22 am
by Roja
d11wtq wrote:Did you do a full copy & paste? Seems like you have a parens missing or something :?
I'm wondering the same thing - I copy/pasted both, and they both run fine locally.

Posted: Mon Mar 28, 2005 11:32 am
by dard
That's so weird... I copied and pasted again and still get errors! In the first one I get a parse error on line 2.

I'm so confused...

Posted: Mon Mar 28, 2005 11:46 am
by Chris Corbyn
Is that the full file? So line 2 really IS what shows as line 2 in this forum?

Posted: Mon Mar 28, 2005 11:50 am
by dard
yes. the file is simply what you see. i was just playing with the code to get it working and then was going to insert where appropriate.

can you cut and paste what you are using that is working? who knows... it might just work.

Posted: Mon Mar 28, 2005 4:28 pm
by dard
Solved! I retyped in the entry and it works beautifully now. There must have been an invisible character causing problems.

Sorry to take up your time - but really appreciate the help!

Posted: Mon Mar 28, 2005 4:35 pm
by feyd
let me guess.. Dreamweaver? It has a tendancy to do that..

Posted: Mon Mar 28, 2005 4:42 pm
by dard
you guessed it! that's a valuable piece of information - i'll be sure to keep that in mind for the future :-)

Posted: Fri Apr 01, 2005 2:33 pm
by squatchimo
feyd wrote:let me guess.. Dreamweaver? It has a tendancy to do that..
Never paste directly from the source into DW. Always dump to Notepad first and then copy from Notepad into DW.

Posted: Fri Apr 01, 2005 3:57 pm
by neophyte
Never paste directly from the source into DW. Always dump to Notepad first and then copy from Notepad into DW.
I've noticed this problem too. One other rule. Never code on a Mac... :x

Posted: Fri Apr 01, 2005 6:39 pm
by John Cartwright
what's wrong with coding on a MAC?

Posted: Fri Apr 01, 2005 7:00 pm
by Roja
This thread has forked so far off its original topic, its beyond recognition. :)