shuffle() not very random?
Posted: Mon Jun 18, 2012 4:25 pm
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:
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?
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);
}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?