shuffle() not very random?

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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

shuffle() not very random?

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: shuffle() not very random?

Post 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.
(#10850)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: shuffle() not very random?

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: shuffle() not very random?

Post by shiznatix »

Thanks, I will try out array_rand. Why would shuffle be giving such consistent, predictable results though?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: shuffle() not very random?

Post 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.
(#10850)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: shuffle() not very random?

Post by VladSun »

Which PHP version is used?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: shuffle() not very random?

Post 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)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: shuffle() not very random?

Post by requinix »

For the record, what are the values in $shows?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: shuffle() not very random?

Post 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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: shuffle() not very random?

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: shuffle() not very random?

Post 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.
Post Reply