{"id":3254,"date":"2018-09-01T00:01:08","date_gmt":"2018-09-01T07:01:08","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3254"},"modified":"2018-09-08T07:50:58","modified_gmt":"2018-09-08T14:50:58","slug":"shuffle-that-playlist","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3254","title":{"rendered":"Shuffle That Playlist"},"content":{"rendered":"<p>Recently, I created a playlist of songs on a certain online subscription service. I chose to shuffle the tunes, but found that one song in particular played more often than the others. My immediate thought was, &#8220;Why can&#8217;t the programmers design a shuffled playlist that doesn&#8217;t overplay the same song&#8221;? Rather than email the programmers, I thought I&#8217;d present the puzzle as this month&#8217;s Exercise.<br \/>\n<!--more--><br \/>\nThe challenge is this (and I hope online music jukebox programmers are visiting this site):<\/p>\n<ul>\n<li>Given a list of 20 songs, randomize their play order.<\/li>\n<li>Ensure that each song doesn&#8217;t play within a range of 15 songs from itself.<\/li>\n<\/ul>\n<p>I chose the value 15 because if you use 20, or the exact number of songs, the same block of 20 tunes repeats in the same pattern. By keeping a 15-song buffer, you ensure that the list is randomized enough to keep things interesting, though some tunes may still play more often than others. That&#8217;s okay.<\/p>\n<p>To get your started, I present the following code. It lists 20 songs in the <code>tune[]<\/code> pointer array. A <code>frequency[]<\/code> array tracks how many times a tune plays based on its element number from the <code>tune[]<\/code> pointer array:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\n#define COUNT 20\r\n\r\nint main()\r\n{\r\n    char *tune[COUNT] = {\r\n        \"Brandy\", \"Respect\", \"Hey Jude\", \"The Twist\",\r\n        \"Uptown Funk\", \"Macarena\", \"Killer Queen\", \"Lady\",\r\n        \"Foolish Games\", \"Call Me\", \"Royals\", \"The Sign\",\r\n        \"Happy\", \"Billie Jean\", \"My Sharona\", \"Family Affair\",\r\n        \"Hurts So Good\", \"Smooth\", \"Mack The Knife\", \"No One\"\r\n    };\r\n    int frequency[COUNT];\r\n    int t,play,cycle;\r\n\r\n    <span class=\"comments\">\/* initialize stuff *\/<\/span>\r\n    srand( (unsigned)time(NULL) );    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    for(t=0;t&lt;COUNT;t++)            <span class=\"comments\">\/* zero frequency counter *\/<\/span>\r\n        frequency[t] = 0;\r\n    cycle = 0;                        <span class=\"comments\">\/* counts to 100 *\/<\/span>\r\n\r\n    <span class=\"comments\">\/* Randomly play the tunes and keep track *\/<\/span>\r\n    while(cycle &lt; 100)\r\n    {\r\n        play = rand() % COUNT;\r\n        printf(\"%3d Playing: %s\\n\",cycle+1,tune[play]);\r\n        frequency[play]++;\r\n        cycle++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* Display frequency list *\/<\/span>\r\n    puts(\"Play Frequency:\");\r\n    for(t=0;t&lt;COUNT;t++)\r\n        printf(\"%s, %d\\n\",\r\n                tune[t],\r\n                frequency[t]\r\n              );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>At Line 28, a random number is chosen from 0 to <code>COUNT<\/code>. The tune is &#8220;played&#8221; or output at Line 29. The element representing the random tune, <code>play<\/code>, is incremented in the <code>frequency[] <\/code>array at Line 30. Line 31 increments the <code>cycle<\/code> counter.<\/p>\n<p>When the code has run through 100 random tunes, the contents of the <code>frequency[]<\/code> array are displayed, indicating how many times each tune played:<\/p>\n<pre><code>...\r\n 99 Playing: Lady\r\n100 Playing: Smooth\r\nPlay Frequency:\r\nBrandy, 5\r\nRespect, 6\r\nHey Jude, 5\r\nThe Twist, 4\r\nUptown Funk, 6\r\nMacarena, 2\r\nKiller Queen, 6\r\nLady, 2\r\nFoolish Games, 2\r\nCall Me, 8\r\nRoyals, 3\r\nThe Sign, 3\r\nHappy, 7\r\nBillie Jean, 6\r\nMy Sharona, 9\r\nFamily Affair, 6\r\nHurts So Good, 3\r\nSmooth, 8\r\nMack The Knife, 5\r\nNo One, 4<\/code><\/pre>\n<p>In the sample output above, you see that <em>My Sharona<\/em> played 9 times during the cycle but <em>Lady<\/em> played only twice. Upon reviewing the full output, I see how several tunes played twice in a row. That&#8217;s merely the nature of pseudo random numbers &mdash; unless you can track them and prevent them from repeating.<\/p>\n<p>Use the code presented here as a base, or craft your own, to limit a song from playing within 15 songs from itself. For example, if <em>My Sharona<\/em> is the 3rd tune played, it can play again until after the cycle count reaches 18.<\/p>\n<p>Please try this exercise on your own before you check out <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3266\">my solution.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write code that ensure two songs in a playlist aren&#8217;t repeated until the entire list is processed. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3254\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3254","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3254"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3254\/revisions"}],"predecessor-version":[{"id":3290,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3254\/revisions\/3290"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}