Proper use of shuffle() array function

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

Post Reply
mgelinas
Forum Newbie
Posts: 13
Joined: Tue Jun 10, 2003 6:33 am
Location: QC Canada

Proper use of shuffle() array function

Post by mgelinas »

I spent quite some time fighting with the shuffle() function to get it to randomize an array of 36 one-character elements. Finally, I found out that using the srand() function prior to calling shuffle() does nothing to the randomization. The best way to randomize an array is to use shuffle() repeatedly on the same array.

If you keep shuffling the same array the same number of times, you basically get the same end result. Again, calling srand() is irrelevant.

Anybody had the same experience? Or really knows how to make shuffle() behave as advertised (i.e., as the manual says)?
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

what version of PHP? after a certain version randomization was automatically seeded on first call... I dont know the shuffle function but I do know that without a seed and a older PHP version you often get very similar results from rand and mt_rand..
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

I'm running a security/bug-patched ancient version (4.1.2, sigh joys of debian stable.......)

The following code works for me:

Code: Select all

$foo = array(1,2,3,4,5,6,7,8,9);
shuffle($foo);
print_r($foo);
Successive reloads show a different order every time and I didn't even have to seed it....

What platform are you on? I remember hearing once that there were some issues on Windows, but I thought they were fixed about 15 months ago....but maybe not....
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

I think the Debian package managers implement some of the newer features in old PHP's if they are security related, and random seeding could be described as security? I wonder how many years until PHP5 reaches Woody, Perhaps not until 4.0 and woody is ancient? :)

It was nearly 3 years ago I ran some tests on randomization on my hosting server, at the time I believe it was a RH5.2 system with PHP4.0.6 or something just around there, and there was clearly repetetive patterns without proper seeding..

If shuffle works good just like that for Eric, as he does I would also suspect platform issues or something being very old or unpatched..
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Off-topic: PHP5 will never reach Woody, maybe Sarge if we're really lucky.... Woody is the current Debian stable, therefore it can only receive security fix upgrades, no version upgrades allowed....
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

Ah, I didnt know that version upgrades does not happen in stable.. hmm I guess I have to install Mozilla or Firebird myself or be stuck with 1.0.0 for a couple of years.. :)
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Of course Debian Testing (the middle distrubition) is more stable than most distro's "stable", normally speaking it has near-current versions and for most people its stable enough, even for "production" use (if 2-4 9's is enough). Sadly its PHP is still only a patched 4.1.2 (same as stable). Unstable is only tracking 4.2.3 now, which is very wierd. Normally unstable is bleeding edge..... I would have though a 4.3.x branche, plus an experimental php5 would be there starting to get the kinks worked out...

Maybe I need to see what I can do to help resolve the problems....
Post Reply