in reply to Re: Music shuffling
in thread Music shuffling

What a great analysis - thank you and ++! My problem here, I suppose, is that I'm not much of a mathematician (hangs head in shame) - I know how to do a frequency count - I just didn't think of doing one to analyze this thing for fairness.

That said. Shuffling once and then just picking from the front would seem to be easier.

Wouldn't that still leave a possible collision problem at the point where the two lists join? Or did I misunderstand what you're describing?


-- 
Human history becomes more and more a race between education and catastrophe. -- HG Wells

Replies are listed 'Best First'.
Re^3: Music shuffling
by BrowserUk (Patriarch) on Jul 08, 2008 at 16:49 UTC
    Wouldn't that still leave a possible collision problem at the point where the two lists join?

    Which two lists? ;)

    Assuming a single list of tracks:

    use List::Util qw[ shuffle ]; my @tracks = shuffle readTracks( 'myMusicDB' ); while( $listening ) { push @tracks, ( my $nextTrack = shift @tracks ); play $nextTrack; }

    By shuffling all your tracks once, when you load them, and then picking from the front (shifting) and replacing at the back (pushing), you are guarenteed to never hear the same track again until you've heard every other track. And you'll never know what track is coming next in any given cycle.

    If your list is short and you resent being able to predict what you will hear next when the loop repeats, you could always remember the first track (post shuffle) and re-shuffle when it comes around again.

    Of course that leaves you open to the possibility of hearing the same track twice in succession when you re-shuffle. Which I guess is what you mean by "two lists".

    My math is pretty much limited to "arithmetic", also. That is, without I go look up the particular more advanced stuff I need for any particular thing. A side-effect of of a career of general programming as opposed to consitantly working in a particular field. A bit like a GP versus a consultant. You may cover much of the ground briefly during training, but you either use it, or loose it!


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Which two lists? ;)

      I think you got it. :) I was, of course, speaking of any two successive lists resulting from the shuffling.

      The randomizing approach I mentioned in my response to jethro would deal with that, though. Here's a shot at it in code:

      #!/usr/bin/perl -w use strict; use List::Util qw[ shuffle ]; my @tracks = 1..100; my $range = 5; # No overlap for this number of songs { my @last; for (@tracks){ unshift @last, $_; @last = @last[0..4] if @last > 5; # Play it! print "$_\n"; } my %h; do { # print "Shuffling...\n"; @tracks = shuffle @tracks; @h{@last,@tracks[0..4]} = (1) x $range * 2; } while keys %h < $range * 2; # For demo purposes... sleep 5; redo; }

      The repeated shuffling mechanism kicks off a lot if $range is close to @tracks / 2; that makes for a good test.

      
      -- 
      Human history becomes more and more a race between education and catastrophe. -- HG Wells