Page 1 of 1

Proper use of shuffle() array function

Posted: Wed Jul 30, 2003 9:32 pm
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)?

Posted: Wed Jul 30, 2003 10:35 pm
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..

Posted: Wed Jul 30, 2003 10:57 pm
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....

Posted: Thu Jul 31, 2003 8:01 am
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..

Posted: Thu Jul 31, 2003 8:52 am
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....

Posted: Thu Jul 31, 2003 11:01 am
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.. :)

Posted: Thu Jul 31, 2003 11:07 am
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....