Generating random unique numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

dard
Forum Newbie
Posts: 6
Joined: Sun Mar 27, 2005 10:50 pm

Generating random unique numbers

Post 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!
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: Generating random unique numbers

Post 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. :)
dard
Forum Newbie
Posts: 6
Joined: Sun Mar 27, 2005 10:50 pm

Post 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!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Did you do a full copy & paste? Seems like you have a parens missing or something :?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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.
dard
Forum Newbie
Posts: 6
Joined: Sun Mar 27, 2005 10:50 pm

Post 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...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Is that the full file? So line 2 really IS what shows as line 2 in this forum?
dard
Forum Newbie
Posts: 6
Joined: Sun Mar 27, 2005 10:50 pm

Post 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.
dard
Forum Newbie
Posts: 6
Joined: Sun Mar 27, 2005 10:50 pm

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

let me guess.. Dreamweaver? It has a tendancy to do that..
dard
Forum Newbie
Posts: 6
Joined: Sun Mar 27, 2005 10:50 pm

Post by dard »

you guessed it! that's a valuable piece of information - i'll be sure to keep that in mind for the future :-)
squatchimo
Forum Commoner
Posts: 28
Joined: Thu Feb 03, 2005 3:36 pm

Post 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.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

what's wrong with coding on a MAC?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

This thread has forked so far off its original topic, its beyond recognition. :)
Post Reply