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)?
Proper use of shuffle() array function
Moderator: General Moderators
I'm running a security/bug-patched ancient version (4.1.2, sigh joys of debian stable.......)
The following code works for me:
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....
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);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....
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..
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..
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....
Maybe I need to see what I can do to help resolve the problems....