<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/sow-the-seeds-know-the-seeds/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://maelle.github.io/2017/04/12/seeds/"> Maëlle</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>When you do simulations, for instance in R, e.g. drawing samples from a distribution, it’s best to set a random seed via the function <code>set.seed</code> in order to have reproducible results. The function has no default value. I think I mostly use <code>set.seed(1)</code>. Last week I received an R script from a colleague in which he used a weird number in <code>set.seed</code> (maybe a phone number? or maybe he let his fingers type randomly?), which made me curious about the usual seed values. As in my blog post <a target="_blank" rel="nofollow" href="http://www.masalmon.eu/2017/02/21/firstcommit/">about initial commit messages</a> I used the Github API via the <a target="_blank" rel="nofollow" href="https://github.com/r-pkgs/gh"><code>gh</code> package</a> to get a very rough answer (an answer seedling from the question seed?).</p>
<p><span></span></p>
<p>From Github API search endpoint you can get up to 1,000 results corresponding to a query which in the case of <code>set.seed</code> occurrences in R code isn’t the whole picture but hopefully a good sample. I wrote a function to treat the output of a query to the API where I take advantage of the <a target="_blank" rel="nofollow" href="https://github.com/tidyverse/stringr"><code>stringr</code> package</a>. I just want the thing inside <code>set.seed()</code> from the text matches returned by the API.</p>
<div>
<pre><code><span>get_seeds_from_matches</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>item</span><span>){</span><span>
</span><span>url</span><span> </span><span>&lt;-</span><span> </span><span>item</span><span>$</span><span>html_url</span><span>
</span><span>matches</span><span> </span><span>&lt;-</span><span> </span><span>item</span><span>$</span><span>text_matches</span><span>
</span><span>matches</span><span> </span><span>&lt;-</span><span> </span><span>unlist</span><span>(</span><span>lapply</span><span>(</span><span>matches</span><span>,</span><span> </span><span>&quot;[[&quot;</span><span>,</span><span> </span><span>&quot;fragment&quot;</span><span>))</span><span>
</span><span>matches</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_split</span><span>(</span><span>matches</span><span>,</span><span> </span><span>&quot;\\\n&quot;</span><span>,</span><span> </span><span>simplify</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>matches</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_extract</span><span>(</span><span>matches</span><span>,</span><span> </span><span>&quot;set\\.seed\\(.*\\)&quot;</span><span>)</span><span>
</span><span>matches</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>matches</span><span>,</span><span> </span><span>&quot;set\\.seed\\(&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>)</span><span>
</span><span>seeds</span><span> </span><span>&lt;-</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>matches</span><span>,</span><span> </span><span>&quot;\\).*&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>)</span><span>
</span><span>seeds</span><span> </span><span>&lt;-</span><span> </span><span>seeds</span><span>[</span><span>!</span><span>is.na</span><span>(</span><span>seeds</span><span>)]</span><span>
</span><span>tibble</span><span>::</span><span>tibble</span><span>(</span><span>seed</span><span> </span><span>=</span><span> </span><span>seeds</span><span>,</span><span>
</span><span>url</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>url</span><span>,</span><span> </span><span>length</span><span>(</span><span>seeds</span><span>)))</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
<p>After that I made the queries themselves, pausing every 30 pages because of the rate limiting, and adding a <code>try</code> around the call in order to stop as soon as I reached the 1,000 results. Not a very elegant solution but I wasn’t in a perfectionnist mood.</p>
<p>Note that the header <code>&quot;Accept&quot; = 'application/vnd.github.v3.text-match+json'</code> is very important, without it you wouldn’t get the text fragments in the results.</p>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;gh&quot;</span><span>)</span><span>
</span><span>seeds</span><span> </span><span>&lt;-</span><span> </span><span>NULL</span><span>
</span><span>ok</span><span> </span><span>&lt;-</span><span> </span><span>TRUE</span><span>
</span><span>page</span><span> </span><span>&lt;-</span><span> </span><span>1</span><span>
</span><span>while</span><span>(</span><span>ok</span><span>){</span><span>
</span><span>matches</span><span> </span><span>&lt;-</span><span> </span><span>try</span><span>(</span><span>gh</span><span>(</span><span>&quot;/search/code&quot;</span><span>,</span><span> </span><span>q</span><span> </span><span>=</span><span> </span><span>&quot;set.seed&amp;language:r&quot;</span><span>,</span><span>
</span><span>.token</span><span> </span><span>=</span><span> </span><span>Sys.getenv</span><span>(</span><span>&quot;GITHUB_PAT&quot;</span><span>),</span><span>
</span><span>.send_headers</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Accept&quot;</span><span> </span><span>=</span><span> </span><span>'application/vnd.github.v3.text-match+json'</span><span>),</span><span>
</span><span>page</span><span> </span><span>=</span><span> </span><span>page</span><span>),</span><span> </span><span>silent</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>ok</span><span> </span><span>&lt;-</span><span> </span><span>!</span><span>is</span><span>(</span><span>matches</span><span>,</span><span> </span><span>&quot;try-error&quot;</span><span>)</span><span>
</span><span>if</span><span>(</span><span>ok</span><span>){</span><span>
</span><span>seeds</span><span> </span><span>&lt;-</span><span> </span><span>bind_rows</span><span>(</span><span>seeds</span><span>,</span><span> </span><span>bind_rows</span><span>(</span><span>lapply</span><span>(</span><span>matches</span><span>$</span><span>items</span><span>,</span><span> </span><span>get_seeds_from_matches</span><span>)))</span><span>
</span><span>}</span><span>
</span><span>page</span><span> </span><span>&lt;-</span><span> </span><span>page</span><span> </span><span>+</span><span> </span><span>1</span><span>
</span><span># wait 2 minutes every 30 pages
</span><span> </span><span>if</span><span>(</span><span>page</span><span> </span><span>%%</span><span> </span><span>30</span><span> </span><span>==</span><span> </span><span>1</span><span> </span><span>&amp;</span><span> </span><span>page</span><span> </span><span>></span><span> </span><span>1</span><span>){</span><span>
</span><span>Sys.sleep</span><span>(</span><span>120</span><span>)</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>save</span><span>(</span><span>seeds</span><span>,</span><span> </span><span>file</span><span> </span><span>=</span><span> </span><span>&quot;data/2017-04-12-seeds.RData.RData&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;magrittr&quot;</span><span>)</span><span>
</span><span>load</span><span>(</span><span>&quot;data/2017-04-12-seeds.RData&quot;</span><span>)</span><span>
</span><span>head</span><span>(</span><span>seeds</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>knitr</span><span>::</span><span>kable</span><span>()</span><span>
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>seed</th>
<th>url</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>https://github.com/berndbischl/ParamHelpers/blob/9d374430701d94639cc78db84f91a0c595927189/tests/testthat/helper_zzz.R</td>
</tr>
<tr>
<td>1</td>
<td>https://github.com/TypeFox/R-Examples/blob/d0917dbaf698cb8bc0789db0c3ab07453016eab9/ParamHelpers/tests/testthat/helper_zzz.R</td>
</tr>
<tr>
<td>1</td>
<td>https://github.com/cran/ParamHelpers/blob/92a49db23e69d32c8ae52585303df2875d740706/tests/testthat/helper_zzz.R</td>
</tr>
<tr>
<td>4.0</td>
<td>https://github.com/ACP-KR/AsanAdvR/blob/0517e88efce94266997d680e8b5a7c2a97c9277d/R-Object-Oriented-Programming-master/chapter4/chapter_4_ex11.R</td>
</tr>
<tr>
<td>4.0</td>
<td>https://github.com/ACP-KR/AsanAdvR/blob/0517e88efce94266997d680e8b5a7c2a97c9277d/R-Object-Oriented-Programming-master/chapter4/chapter_4_ex11.R</td>
</tr>
<tr>
<td>4.0</td>
<td>https://github.com/KellyBlack/R-Object-Oriented-Programming/blob/efbb0b81063baa30dd9d56d5d74b3f73b12b4926/chapter4/chapter_4_ex11.R</td>
</tr>
</tbody>
</table>
<p>I got 984 entries, not 1,000 so maybe I lost some seeds in the process or the results weren’t perfect. The reason why I also added the URL of the script to the results was to be able to go and look at the code around surprising seeds.</p>
<p>Let’s have a look at the most frequent seeds in the sample.</p>
<div>
<pre><code><span>table</span><span>(</span><span>seeds</span><span>$</span><span>seed</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>broom</span><span>::</span><span>tidy</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>dplyr</span><span>::</span><span>arrange</span><span>(</span><span>desc</span><span>(</span><span>Freq</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>head</span><span>(</span><span>n</span><span> </span><span>=</span><span> </span><span>12</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>knitr</span><span>::</span><span>kable</span><span>()</span><span>
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>Var1</th>
<th>Freq</th>
</tr>
</thead>
<tbody>
<tr>
<td>seed</td>
<td>312</td>
</tr>
<tr>
<td>1</td>
<td>134</td>
</tr>
<tr>
<td>123</td>
<td>60</td>
</tr>
<tr>
<td>iseed</td>
<td>48</td>
</tr>
<tr>
<td>10</td>
<td>47</td>
</tr>
<tr>
<td>13121098</td>
<td>28</td>
</tr>
<tr>
<td>ss</td>
<td>24</td>
</tr>
<tr>
<td>20</td>
<td>21</td>
</tr>
<tr>
<td>1234</td>
<td>18</td>
</tr>
<tr>
<td>42</td>
<td>18</td>
</tr>
<tr>
<td>123456</td>
<td>15</td>
</tr>
<tr>
<td>0</td>
<td>14</td>
</tr>
</tbody>
</table>
<p>So the most prevalent seed is a mystery because I’m not motivated enough to go scrape the code to find if the seed gets assigned a value before, like <a target="_blank" rel="nofollow" href="https://twitter.com/millerdl/status/852152963502149638">in that tweet I saw today</a>. I was happy that 1 was so popular, maybe it means I belong?</p>
<p>I was surprised by two values. First, 13121098.</p>
<div>
<pre><code><span>dplyr</span><span>::</span><span>filter</span><span>(</span><span>seeds</span><span>,</span><span> </span><span>seed</span><span> </span><span>==</span><span> </span><span>&quot;13121098&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>head</span><span>(</span><span>n</span><span> </span><span>=</span><span> </span><span>10</span><span>)</span><span> </span><span>%>%</span><span> </span><span>knitr</span><span>::</span><span>kable</span><span>()</span><span>
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>seed</th>
<th>url</th>
</tr>
</thead>
<tbody>
<tr>
<td>13121098</td>
<td>https://github.com/DJRumble/Swirl-Course/blob/4e2771141e579904eb6dd32bce51ff6e0d840d44/Regression_Models/Residuals_Diagnostics_and_Variation/initLesson.R</td>
</tr>
<tr>
<td>13121098</td>
<td>https://github.com/swirldev/swirl_courses/blob/b3d432bfdf480c865af1c409ee0ee927c1fdbda0/Regression_Models/Residuals_Diagnostics_and_Variation/initLesson.R</td>
</tr>
<tr>
<td>13121098</td>
<td>https://github.com/1vbutkus/swirl/blob/310874100536e1e7c66861eced9ecb52939a3e0a/Regression_Models/Residuals_Diagnostics_and_Variation/initLesson.R</td>
</tr>
<tr>
<td>13121098</td>
<td>https://github.com/gotitsingh13/swirldev/blob/b7369b974ba76716fbcf6101bcbdc2db2f774d18/Regression_Models/Residuals_Diagnostics_and_Variation/initLesson.R</td>
</tr>
<tr>
<td>13121098</td>
<td>https://github.com/pauloramazza/swirl_courses/blob/4e2771141e579904eb6dd32bce51ff6e0d840d44/Regression_Models/Residuals_Diagnostics_and_Variation/initLesson.R</td>
</tr>
<tr>
<td>13121098</td>
<td>https://github.com/ildesoft/Swirl_Courses/blob/3e7f43cecbeb41e92e4f5972658f9b293e0e4b84/Regression_Models/Residuals_Diagnostics_and_Variation/initLesson.R</td>
</tr>
<tr>
<td>13121098</td>
<td>https://github.com/hrdg/Regression_Models/blob/22f47ecf2ae62f553aa132d3d948cc6b4e1599cc/Residuals_Diagnostics_and_Variation/initLesson.R</td>
</tr>
<tr>
<td>13121098</td>
<td>https://github.com/Rizwanabro/Swirl-Course/blob/3e7f43cecbeb41e92e4f5972658f9b293e0e4b84/Regression_Models/Residuals_Diagnostics_and_Variation/initLesson.R</td>
</tr>
<tr>
<td>13121098</td>
<td>https://github.com/mkgiitr/Data-Analytics/blob/1d659db1e9137b1fe595a6ef3356887de431b1be/win-library/3.1/swirl/Courses/Regression_Models/Residuals_Diagnostics_and_Variation/initLesson.R</td>
</tr>
<tr>
<td>13121098</td>
<td>https://github.com/Jutair/R-programming-Coursera/blob/4faeed6ca780ee7f14b224c293cae77293146f37/Swirl/Rsubversion/branches/Writing_swirl_Courses/Regression_Models/Residuals_Diagnostics_and_Variation/initLesson.R</td>
</tr>
</tbody>
</table>
<p>I went and had a look and it seems most repositories correspond to code learnt in a <a target="_blank" rel="nofollow" href="https://es.coursera.org/learn/data-cleaning">Coursera course</a>. I have taken a few courses from that specialization and loved it but I don’t remember learning about the special seed, too bad. Well I guess everyone used it to reproduce results but what does this number mean in the first place? Who typed it? A cat walking on the keyboard?</p>
<p>The other number that surprised me was 42 but then I remembered it is the <a target="_blank" rel="nofollow" href="https://simple.wikipedia.org/wiki/42_(answer)">“Answer to the Ultimate Question of Life, the Universe, and Everything” </a>. I’d therefore say that this might be the coolest random seed. Now I can’t tell you whether it produces better results. Maybe it helps when your code actually tries to answer the Ultimate Question of Life, the Universe, and Everything?</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://maelle.github.io/2017/04/12/seeds/"> Maëlle</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/rBBDVwPKkNc"></a></div>