Created
March 22, 2017 20:14
Star
You must be signed in to star a gist
Simulation of 'how many tracks before a repetition in my playlist' problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "Playlist" | |
author: "Dan MacKinnon" | |
output: html_document | |
--- | |
```{r setup, include=FALSE} | |
knitr::opts_chunk$set(echo = TRUE) | |
``` | |
##Introduction | |
You may have noticed when shuffling through a playlist, you end up hearing a repeated song well before you might expect to. | |
> When shuffling **n** songs on a play list, how many songs will play before a track repeats? | |
This of course depends on how the songs are suffled: does your playback system have a sophisticated way of detecting and avoiding repetitions? We will assume that the suffle playback is random and does not include any specific repetition avoidance strategies. | |
## Setting up the Simulation | |
```{r} | |
trialsPerGroup <- 100; | |
playListLimit <- 200 | |
trial_index <- 1: (trialsPerGroup*playListLimit) | |
PlayList <- data.frame(trial_index) | |
``` | |
Here we have created a data frame that looks at playlists up to a size of `r playListLimit` and for each size of playlist runs `r trialsPerGroup` experiments to see when the first repetition happens. | |
```{r} | |
current_index <- 1; | |
for (i in 1:playListLimit) { | |
for (k in 1:trialsPerGroup) { | |
t <- sample(1:i, i, replace=TRUE) | |
PlayList$list[current_index] <- list(k) | |
PlayList$size[current_index] <- i | |
PlayList$firstRepeat[current_index] <- anyDuplicated(t) | |
current_index <- current_index +1 | |
} | |
} | |
PlayListSummary <- data.frame(1:playListLimit) | |
for (i in 1:playListLimit) { | |
PlayListSummary$size[i] <- i | |
PlayListSummary$avgTimeToRepeat[i] <- mean(subset(PlayList, PlayList$size == i)$firstRepeat) | |
} | |
``` | |
## Exploring the Simulation | |
```{r} | |
plot(PlayListSummary$size,PlayListSummary$avgTimeToRepeat,main="tracks before repetition", ylab="first repetition", xlab="available tracks" ) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment