Skip to content

Instantly share code, notes, and snippets.

@dmackinnon1
Created March 22, 2017 20:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dmackinnon1/09fd6a0cd625ff31494ea23fc253fb20 to your computer and use it in GitHub Desktop.
Simulation of 'how many tracks before a repetition in my playlist' problem
---
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