### iPod Shuffle Math

I noticed that my iPod Shuffle repeated songs (in its shuffle mode) more often than I would have expected. But I thought about it a bit. Then I thought a bit more. Then … aha, said I: this is the Birthday Paradox. Which is (to quote Wikipedia): "if there are 23 people in a room then there is a chance of more than 50% that at least two of them will have the same birthday." That's a lot better chance than our untutored intuition would tell us. The same is true with the Shuffle: relatively short sequences of randomly-selected songs contain repeats more often than you'd think.

If my iPod held 365 songs, then it's exactly the same math as the Paradox: about 23 randomly-selected songs would have a better-than-even chance of containing a repeat.

But (right now) it's holding 262 songs, totalling about 17.7 hours of music. So it's clear that makes repeats more likely with songs than with birthdays, right? But how much more? I wrote a script, based on the Wikipedia entry, to do the math:

```#!/usr/bin/perl

use strict;
use warnings;

print "Enter number of shuffled songs: ";
chomp( my \$d = <> );
print "Enter total playtime (in hours): ";
chomp( my \$tot_time = <> );
print "Enter cutoff probability: ";
chomp( my \$cutoff = <> );
my \$prob = 1;
my \$n    = 1;
while ( 1.0 - \$prob < \$cutoff ) {
\$prob *= ( 1.0 - \$n++ / \$d );
}
printf "Probability: %0.3f that\n",                    1.0 - \$prob;
printf "%d randomly selected songs\n",                 \$n;
printf "out of %d will contain at least one repeat\n", \$d;
printf "Approx time for %d songs: %0.1f hours\n",      \$n, \$n / \$d * \$tot_time;
```

No comments on the code, please, just a quick and dirty hack. Results: Out of 20 randomly-selected songs out of 262, there are better-than-even odds that a repeat will occur. (This is about 80 minutes worth of music, since each song averages about four minutes.)

Increasing to 35 random songs (about 2.4 hours worth), the probability of a repeat goes over 90%!

Math is fun and surprising.