Page 1 of 1

shuffle() not very random?

Posted: Mon Jun 18, 2012 4:25 pm
by shiznatix
So, I have this script that ssh connects to my desktop computer, goes through some HDs, gets all the video files, puts them into an array, (this is the important part) shuffles the array, and lastly takes 5 items to play on the media player.

The script itself works just fine, no problems there. The problem is that the shuffle() doesn't seam to really be random. I keep getting the same results at a rate that makes me think this function is no good. The original array has 2727 entries but about 20 files have a tendency to always show up in the 5 random results. I know for sure because with the rate that I use this script, it should take me hundreds of runs to get the same result twice but I had 3 entries show up twice in 3 runs. Crazy annoying.

My code is thus:

Code: Select all

shuffle($shows);//$shows has the 2727 entries

$playlistShows = array();//just trying to get 5 results in the end

for ($i = 0; $i < registry::fetchKey('playlistSize'); $i++)
{
	$playlistShows[] = $shows[$i];
	unset($shows[$i]);
	
	shuffle($shows);
}
I tried running the script a few thousand times and noticed that some files got played ~20 times while others got 2 plays. I don't know enough about statistics to know how normal this is but just figured I would throw that out there.

Is there a better way to get 5 random results out of an array that large? I have a tendency to run the script at the same time every day (before I go to bed) so if it somehow took the current time of some sort as the seed then maybe that has something to do with it? Maybe I am just going insane?

Re: shuffle() not very random?

Posted: Mon Jun 18, 2012 4:34 pm
by Christopher
I wonder if the re-shuffling is causing it. Have you tried array_rand()? That would simplify way you are trying to do.

Re: shuffle() not very random?

Posted: Mon Jun 18, 2012 9:48 pm
by requinix
Shuffle it just the one time before the loop.

Also array_rand() will pick five random entries but they won't be in a random order. If they have to be shuffled then I suggest

Code: Select all

$playlistShows = array_rand($shows, registry::fetchKey('playlistSize'));
shuffle($playlistShows);
And no, shuffling before or after won't make a difference.

Re: shuffle() not very random?

Posted: Tue Jun 19, 2012 3:14 pm
by shiznatix
Thanks, I will try out array_rand. Why would shuffle be giving such consistent, predictable results though?

Re: shuffle() not very random?

Posted: Tue Jun 19, 2012 5:02 pm
by Christopher
shiznatix wrote:Why would shuffle be giving such consistent, predictable results though?
My guess was that you are re-shuffling a lot in a very consistent and determinate way. But I don't know.

Re: shuffle() not very random?

Posted: Fri Jun 22, 2012 2:03 am
by VladSun
Which PHP version is used?

Re: shuffle() not very random?

Posted: Sun Jul 08, 2012 6:11 pm
by shiznatix
So, it seams that array_rand is not very random either. I am noticing the same patterns as before with the same shows in the top 5 over and over. It isnt the same files as when I was only using shuffle, but still there is a repeating problem.

My exact code is:

Code: Select all

$playlistShows = array_rand($shows, registry::fetchKey('playlistSize'));
shuffle($playlistShows);
And I am running php version 5.3.3-7 (debian squeeze)

Re: shuffle() not very random?

Posted: Sun Jul 08, 2012 9:00 pm
by requinix
For the record, what are the values in $shows?

Re: shuffle() not very random?

Posted: Mon Jul 09, 2012 4:40 pm
by shiznatix
requinix wrote:For the record, what are the values in $shows?
*ahem* this is just a random idea of what my original $shows could possibly look like, in a world where it might look like that (not this one) *ahem*
http://pastebin.com/UeGvedzX

Re: shuffle() not very random?

Posted: Tue Jul 17, 2012 5:15 pm
by shiznatix
*bump*

It is actually worse now that I started using array_rand. Is there any way to shuffle a large array with real (or at least close to real) randomization?

Re: shuffle() not very random?

Posted: Wed Jul 18, 2012 7:44 pm
by Weirdan
Isn't this a form of classical birthday paradox? I'd suggest you keep your watching history for about a month and exclude shows you watched recently when choosing shows to watch.