Episode Generator

Sometimes, I like to watch random episodes of my favorite shows instead of watching them in order. In this article I will use Friends on Netflix as an example. I used to do this manually by looking up a list of episodes to see how many there are (236 for Friends), then I would generate a random number between 1-236 and finally I would look up the episode on Netflix.

My idea for this post is to use JavaScript to do all three steps automatically. What it does is each time you refresh, it will generate a new random number and you can click on the link to view it directly on Netflix. So refresh and try it out.

Refresh for a Random Friends Episode Link


Each time you refresh you will see that the episode number will change (a number between 1 and 236) and the Netflix link will be updated correspondingly.

To add this to your own website, add a div to your website:

          <div id="randomDiv" >
            <h2>Refresh for a Random Friends Episode Link</h2>

Then add the following JavaScript code at the end of your body:

          function generateRandomEpisode(htmlContent, numberOfEpisodes, idList, divName, episodesPerSeason) {
            //htmlContent is the new content that will eventually contain our link
            //numberOfEpisodes is the total number of episodes of your show (see wikipedia)
            //idList is a list of all the Netflix identifiers, which you can find in the Netflix URLs
            //divName is the name of the div that you would like to replace the content of
            //episodesPerSeason is a list of episodes per season, which is used to derive which season and episode came out of the result.
            //generates a random number between 0 and numberOfEpisodes-1 inclusive of bounds.
            var randomOffset = Math.floor(Math.random()*numberOfEpisodes);
            //Netflix shows always start with this URL and then an identifier
            var baseURL = "https://www.netflix.com/watch/"
            //We can use the randomOffset we generated to get the netflix ID that we need in the idList.
            var generatedEpisodeID = idList[randomOffset];
            //Complete our generated URL:
            baseURL += generatedEpisodeID.toString();
            //Find out season and episode number
            var season = 1;
            var episode = randomOffset + 1; // This is because randomOffset starts from 0, while we usually count episodes from 1.
            //Now we run through the episodesPerSeason list to find out which season and episode we've found.
            for (var i = 0; i < episodesPerSeason.length; i++) {
              if (episode <= episodesPerSeason[i]) {
              season += 1;
              episode -= episodesPerSeason[i];
            //Append our URL and episode number to the htmlContent.
            htmlContent += '<p> <a href="'+baseURL+'">Click here to watch your randomly generated episode on Netflix</a>. It is season ' + season.toString() + ' and episode '+episode.toString()+'.</p>';
            //Finally we actually show our htmlContent on the website.
            document.getElementById(divName).innerHTML = htmlContent;

          //Start of code that gets executed directly.
          //This generates the episodes of friends. Since friends got added all at once, the identifiers are all consecutive.
          var friendIDs = [];
          for (var i = 0; i < 236; i++) {
            friendIDs.push(i + 70273997);
          //There are a couple of exceptions in the friends episodes, where they combine two parts
          friendIDs[216] = 80057887; //S09E23
          friendIDs[217] = friendIDs[216]; //S09E24
          friendIDs[235] = friendIDs[234]; //S10E180
          //Actually call our above function.
          generateRandomEpisode("<h2>Refresh for a Random Friends Episode Link</h2>", friendIDs.length, friendIDs, "randomDiv", [24,24,25,24,24,25,24,24,24,18]);

You can change this code to suite your own show by generating the inputs necessary for the generateRandomEpisode function. htmlContent should contain the show name in a header environment. numberOfEpisodes should contain the total number of episodes. idList should be a list of Netflix identifers, which can usually be generated because all episodes in a season are consecutive (see the example code). divName is the name of the div that you would like the javascript to replace and episodesPerSeason is a list of how many episodes are in each season, which can be used to calculate the season and episode number.

Refresh for a Random Rick and Morty Link