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
Post
by dard » Sun Mar 27, 2005 10:55 pm
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
Post
by Roja » Sun Mar 27, 2005 11:18 pm
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.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Mon Mar 28, 2005 8:01 am
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 » Mon Mar 28, 2005 11:22 am
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 » Mon Mar 28, 2005 11:32 am
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...
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Mon Mar 28, 2005 11:46 am
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 » Mon Mar 28, 2005 11:50 am
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 » Mon Mar 28, 2005 4:28 pm
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!
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Mar 28, 2005 4:35 pm
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 » Mon Mar 28, 2005 4:42 pm
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 » Fri Apr 01, 2005 2:33 pm
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.
neophyte
DevNet Resident
Posts: 1537 Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota
Post
by neophyte » Fri Apr 01, 2005 3:57 pm
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...
Roja
Tutorials Group
Posts: 2692 Joined: Sun Jan 04, 2004 10:30 pm
Post
by Roja » Fri Apr 01, 2005 7:00 pm
This thread has forked so far off its original topic, its beyond recognition.