Alice, Bob, and Mallory: Inspired by randomnessmetasyntacticstag:alicebobandmallory.com,2005:TypoTypo2012-03-22T22:47:11+01:00Jonas Elfströmurn:uuid:f596c241-0004-4935-ba79-64693f890cb22011-10-27T23:19:00+02:002012-03-22T22:47:11+01:00Inspired by randomness<p>Inspired by <a href="http://vanillajava.blogspot.com/2011/10/randomly-no-so-random.html">Randomly not so random</a> I decided to play around with the subject.</p>
<p>Both Java and C# uses a <a href="http://en.wikipedia.org/wiki/Linear_congruential_generator">linear congruential generator</a> as their <a href="http://en.wikipedia.org/wiki/Pseudorandom_number_generator">pseudorandom number generators</a>. </p>
<blockquote>
<p>The current implementation of the Random class is based on a modified version of Donald E. Knuth's subtractive random number generator algorithm."</p>
</blockquote>
<p>About System.Random at <a href="http://msdn.microsoft.com/en-us/library/system.random.aspx">MSDN</a>.</p>
<p>I tried to find information on what values Microsoft uses for the <em>m</em>, <em>a</em>, and <em>c</em> parameters in their <a href="http://en.wikipedia.org/wiki/Linear_congruential_generator">LGC</a> implementation but I failed to do so. </p>
<p>A good random number generator should have certain properties. </p>
<blockquote>
<p>The German Federal Office for Information Security (BSI) has established four criteria for quality of deterministic random number generators. They are summarized here:<br/>
K1 — A sequence of random numbers with a low probability of containing identical consecutive elements. </p>
</blockquote>
<p>From the <a href="http://en.wikipedia.org/wiki/Pseudorandom_number_generator">pseudorandom number generator</a> article on Wikipedia. Let's see what we can do about that.</p>
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a>
<a href="#n2" name="n2">2</a>
<a href="#n3" name="n3">3</a>
<a href="#n4" name="n4">4</a>
<a href="#n5" name="n5">5</a>
</pre></td>
<td class="code"><pre>var random = new Random(<span style="color:#00D">116793166</span>);
<span style="color:#080;font-weight:bold">for</span> (<span style="color:#0a5;font-weight:bold">int</span> i = <span style="color:#00D">0</span>; i < <span style="color:#00D">9</span>; i++)
{
Console.Write(random.Next(<span style="color:#00D">10</span>) + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20"> </span><span style="color:#710">"</span></span>);
}</pre></td>
</tr></table>
<p><br/></p>
<p>Outputs:</p>
<pre>
1 1 1 1 1 1 1 1 1
</pre>
<p>Did I just break the K1 criteria above? You know I didn't but you might be interested in how I found the seed number. It's easy but also quite computational intensive, that's why I used <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.for.aspx">Parallel.For</a>.</p>
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a>
<a href="#n2" name="n2">2</a>
<a href="#n3" name="n3">3</a>
<a href="#n4" name="n4">4</a>
<a href="#n5" name="n5">5</a>
<a href="#n6" name="n6">6</a>
<a href="#n7" name="n7">7</a>
<a href="#n8" name="n8">8</a>
<a href="#n9" name="n9">9</a>
<strong><a href="#n10" name="n10">10</a></strong>
<a href="#n11" name="n11">11</a>
<a href="#n12" name="n12">12</a>
</pre></td>
<td class="code"><pre>Parallel.For(<span style="color:#00D">0</span>, <span style="color:#0a5;font-weight:bold">int</span>.MaxValue, i =>
{
var rnd = new Random(i);
<span style="color:#0a5;font-weight:bold">bool</span> allSame = <span style="color:#069">true</span>;
<span style="color:#080;font-weight:bold">for</span> (<span style="color:#0a5;font-weight:bold">int</span> j = <span style="color:#00D">0</span>; j < <span style="color:#00D">9</span>; j++)
{
allSame = allSame && rnd.Next(<span style="color:#00D">10</span>) == <span style="color:#00D">1</span>;
<span style="color:#080;font-weight:bold">if</span> (!allSame) <span style="color:#080;font-weight:bold">break</span>;
}
<span style="color:#080;font-weight:bold">if</span> (allSame)
Console.WriteLine(i);
});</pre></td>
</tr></table>
<p><br/>
It took a couple of minutes for the number 116793166 to show up in my console.</p>
<p>To keep on playing I needed a random string generator. Mine looks like this.</p>
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a>
<a href="#n2" name="n2">2</a>
<a href="#n3" name="n3">3</a>
<a href="#n4" name="n4">4</a>
<a href="#n5" name="n5">5</a>
<a href="#n6" name="n6">6</a>
<a href="#n7" name="n7">7</a>
<a href="#n8" name="n8">8</a>
<a href="#n9" name="n9">9</a>
</pre></td>
<td class="code"><pre>private <span style="color:#088;font-weight:bold">const</span> string _chars = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">abcdefghijklmnopqrstuvwxyz</span><span style="color:#710">"</span></span>;
private <span style="color:#088;font-weight:bold">static</span> string RandomString(<span style="color:#0a5;font-weight:bold">int</span> size, Random rng)
{
var buffer = new <span style="color:#0a5;font-weight:bold">char</span>[size];
<span style="color:#080;font-weight:bold">for</span> (<span style="color:#0a5;font-weight:bold">int</span> i = <span style="color:#00D">0</span>; i < size; i++)
buffer[i] = _chars[rng.Next(_chars.Length)];
<span style="color:#080;font-weight:bold">return</span> new string(buffer);
}</pre></td>
</tr></table>
<p><br/>
If the random generators were the same it looks like it should give the same result as <a href="http://vanillajava.blogspot.com/2011/10/randomly-no-so-random.html">the Java one</a> but <code>RandomString(5, new Random(-229985452))</code> returns <code>tfsld</code>. Either my RandomString method works different than the Java one or it's the case that Java and .NET has slightly different settings of their LGCs.</p>
<p>Mathematically there's an infinite amount of inputs that results in a returned <code>hello</code>, for instance 1382472294, but here we are limited by the size of our integers.</p>
<p>I found the seed 1382472294 with the following little method and loop</p>
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a>
<a href="#n2" name="n2">2</a>
<a href="#n3" name="n3">3</a>
<a href="#n4" name="n4">4</a>
<a href="#n5" name="n5">5</a>
<a href="#n6" name="n6">6</a>
<a href="#n7" name="n7">7</a>
<a href="#n8" name="n8">8</a>
<a href="#n9" name="n9">9</a>
<strong><a href="#n10" name="n10">10</a></strong>
<a href="#n11" name="n11">11</a>
<a href="#n12" name="n12">12</a>
<a href="#n13" name="n13">13</a>
<a href="#n14" name="n14">14</a>
<a href="#n15" name="n15">15</a>
<a href="#n16" name="n16">16</a>
<a href="#n17" name="n17">17</a>
<a href="#n18" name="n18">18</a>
<a href="#n19" name="n19">19</a>
<strong><a href="#n20" name="n20">20</a></strong>
</pre></td>
<td class="code"><pre>private <span style="color:#088;font-weight:bold">static</span> <span style="color:#0a5;font-weight:bold">bool</span> CompareToRandomString(string str, Random rng)
{
<span style="color:#080;font-weight:bold">for</span> (<span style="color:#0a5;font-weight:bold">int</span> i = <span style="color:#00D">0</span>; i < str.Length; i++)
{
<span style="color:#080;font-weight:bold">if</span> (_chars[rng.Next(_chars.Length)] != str[i])
<span style="color:#080;font-weight:bold">return</span> <span style="color:#069">false</span>;
}
<span style="color:#080;font-weight:bold">return</span> <span style="color:#069">true</span>;
}
...
<span style="color:#088;font-weight:bold">const</span> string lookingFor = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">hello</span><span style="color:#710">"</span></span>;
Parallel.For(<span style="color:#00D">0</span>, <span style="color:#0a5;font-weight:bold">int</span>.MaxValue, i =>
{
var rnd = new Random(i);
<span style="color:#080;font-weight:bold">if</span> (CompareToRandomString(lookingFor, rnd))
Console.WriteLine(i);
});</pre></td>
</tr></table>
<p><br/>
I leave it up to you to implement a <a href="http://msdn.microsoft.com/en-us/library/dd460721.aspx">break</a> out of that loop.</p>
<p><a href="http://www.ruby-doc.org/core-1.9.2/Random.html">Ruby</a> does not use a LGC but a <a href="http://en.wikipedia.org/wiki/Mersenne_twister">Mersenne twister</a> instead. It's still only pseudorandom so there's no problem finding patterns you like and being able to repeat them.</p>
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a>
<a href="#n2" name="n2">2</a>
</pre></td>
<td class="code"><pre>srand(<span style="color:#00D">2570940381</span>)
<span style="color:#00D">9</span>.times { print <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">%d </span><span style="color:#710">"</span></span> % rand(<span style="color:#00D">10</span>) }</pre></td>
</tr></table>
<p>Outputs:<br/>
<code>1 2 3 4 5 6 7 8 9</code></p>
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a>
<a href="#n2" name="n2">2</a>
<a href="#n3" name="n3">3</a>
</pre></td>
<td class="code"><pre>charset=(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">a</span><span style="color:#710">'</span></span>..<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">z</span><span style="color:#710">'</span></span>).to_a
srand(<span style="color:#00D">124931</span>)
puts (<span style="color:#00D">0</span>...<span style="color:#00D">4</span>).map{ charset.to_a[rand(charset.size)] }.join</pre></td>
</tr></table>
<p>Outputs:<br/>
<code>ruby</code></p><p>Inspired by <a href="http://vanillajava.blogspot.com/2011/10/randomly-no-so-random.html">Randomly not so random</a> I decided to play around with the subject.</p>
<p>Both Java and C# uses a <a href="http://en.wikipedia.org/wiki/Linear_congruential_generator">linear congruential generator</a> as their <a href="http://en.wikipedia.org/wiki/Pseudorandom_number_generator">pseudorandom number generators</a>. </p>
<blockquote>
<p>The current implementation of the Random class is based on a modified version of Donald E. Knuth's subtractive random number generator algorithm."</p>
</blockquote>
<p>About System.Random at <a href="http://msdn.microsoft.com/en-us/library/system.random.aspx">MSDN</a>.</p>
<p>I tried to find information on what values Microsoft uses for the <em>m</em>, <em>a</em>, and <em>c</em> parameters in their <a href="http://en.wikipedia.org/wiki/Linear_congruential_generator">LGC</a> implementation but I failed to do so. </p>
<p>A good random number generator should have certain properties. </p>
<blockquote>
<p>The German Federal Office for Information Security (BSI) has established four criteria for quality of deterministic random number generators. They are summarized here:<br/>
K1 — A sequence of random numbers with a low probability of containing identical consecutive elements. </p>
</blockquote>
<p>From the <a href="http://en.wikipedia.org/wiki/Pseudorandom_number_generator">pseudorandom number generator</a> article on Wikipedia. Let's see what we can do about that.</p>
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a>
<a href="#n2" name="n2">2</a>
<a href="#n3" name="n3">3</a>
<a href="#n4" name="n4">4</a>
<a href="#n5" name="n5">5</a>
</pre></td>
<td class="code"><pre>var random = new Random(<span style="color:#00D">116793166</span>);
<span style="color:#080;font-weight:bold">for</span> (<span style="color:#0a5;font-weight:bold">int</span> i = <span style="color:#00D">0</span>; i < <span style="color:#00D">9</span>; i++)
{
Console.Write(random.Next(<span style="color:#00D">10</span>) + <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20"> </span><span style="color:#710">"</span></span>);
}</pre></td>
</tr></table>
<p><br/></p>
<p>Outputs:</p>
<pre>
1 1 1 1 1 1 1 1 1
</pre>
<p>Did I just break the K1 criteria above? You know I didn't but you might be interested in how I found the seed number. It's easy but also quite computational intensive, that's why I used <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.for.aspx">Parallel.For</a>.</p>
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a>
<a href="#n2" name="n2">2</a>
<a href="#n3" name="n3">3</a>
<a href="#n4" name="n4">4</a>
<a href="#n5" name="n5">5</a>
<a href="#n6" name="n6">6</a>
<a href="#n7" name="n7">7</a>
<a href="#n8" name="n8">8</a>
<a href="#n9" name="n9">9</a>
<strong><a href="#n10" name="n10">10</a></strong>
<a href="#n11" name="n11">11</a>
<a href="#n12" name="n12">12</a>
</pre></td>
<td class="code"><pre>Parallel.For(<span style="color:#00D">0</span>, <span style="color:#0a5;font-weight:bold">int</span>.MaxValue, i =>
{
var rnd = new Random(i);
<span style="color:#0a5;font-weight:bold">bool</span> allSame = <span style="color:#069">true</span>;
<span style="color:#080;font-weight:bold">for</span> (<span style="color:#0a5;font-weight:bold">int</span> j = <span style="color:#00D">0</span>; j < <span style="color:#00D">9</span>; j++)
{
allSame = allSame && rnd.Next(<span style="color:#00D">10</span>) == <span style="color:#00D">1</span>;
<span style="color:#080;font-weight:bold">if</span> (!allSame) <span style="color:#080;font-weight:bold">break</span>;
}
<span style="color:#080;font-weight:bold">if</span> (allSame)
Console.WriteLine(i);
});</pre></td>
</tr></table>
<p><br/>
It took a couple of minutes for the number 116793166 to show up in my console.</p>
<p>To keep on playing I needed a random string generator. Mine looks like this.</p>
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a>
<a href="#n2" name="n2">2</a>
<a href="#n3" name="n3">3</a>
<a href="#n4" name="n4">4</a>
<a href="#n5" name="n5">5</a>
<a href="#n6" name="n6">6</a>
<a href="#n7" name="n7">7</a>
<a href="#n8" name="n8">8</a>
<a href="#n9" name="n9">9</a>
</pre></td>
<td class="code"><pre>private <span style="color:#088;font-weight:bold">const</span> string _chars = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">abcdefghijklmnopqrstuvwxyz</span><span style="color:#710">"</span></span>;
private <span style="color:#088;font-weight:bold">static</span> string RandomString(<span style="color:#0a5;font-weight:bold">int</span> size, Random rng)
{
var buffer = new <span style="color:#0a5;font-weight:bold">char</span>[size];
<span style="color:#080;font-weight:bold">for</span> (<span style="color:#0a5;font-weight:bold">int</span> i = <span style="color:#00D">0</span>; i < size; i++)
buffer[i] = _chars[rng.Next(_chars.Length)];
<span style="color:#080;font-weight:bold">return</span> new string(buffer);
}</pre></td>
</tr></table>
<p><br/>
If the random generators were the same it looks like it should give the same result as <a href="http://vanillajava.blogspot.com/2011/10/randomly-no-so-random.html">the Java one</a> but <code>RandomString(5, new Random(-229985452))</code> returns <code>tfsld</code>. Either my RandomString method works different than the Java one or it's the case that Java and .NET has slightly different settings of their LGCs.</p>
<p>Mathematically there's an infinite amount of inputs that results in a returned <code>hello</code>, for instance 1382472294, but here we are limited by the size of our integers.</p>
<p>I found the seed 1382472294 with the following little method and loop</p>
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a>
<a href="#n2" name="n2">2</a>
<a href="#n3" name="n3">3</a>
<a href="#n4" name="n4">4</a>
<a href="#n5" name="n5">5</a>
<a href="#n6" name="n6">6</a>
<a href="#n7" name="n7">7</a>
<a href="#n8" name="n8">8</a>
<a href="#n9" name="n9">9</a>
<strong><a href="#n10" name="n10">10</a></strong>
<a href="#n11" name="n11">11</a>
<a href="#n12" name="n12">12</a>
<a href="#n13" name="n13">13</a>
<a href="#n14" name="n14">14</a>
<a href="#n15" name="n15">15</a>
<a href="#n16" name="n16">16</a>
<a href="#n17" name="n17">17</a>
<a href="#n18" name="n18">18</a>
<a href="#n19" name="n19">19</a>
<strong><a href="#n20" name="n20">20</a></strong>
</pre></td>
<td class="code"><pre>private <span style="color:#088;font-weight:bold">static</span> <span style="color:#0a5;font-weight:bold">bool</span> CompareToRandomString(string str, Random rng)
{
<span style="color:#080;font-weight:bold">for</span> (<span style="color:#0a5;font-weight:bold">int</span> i = <span style="color:#00D">0</span>; i < str.Length; i++)
{
<span style="color:#080;font-weight:bold">if</span> (_chars[rng.Next(_chars.Length)] != str[i])
<span style="color:#080;font-weight:bold">return</span> <span style="color:#069">false</span>;
}
<span style="color:#080;font-weight:bold">return</span> <span style="color:#069">true</span>;
}
...
<span style="color:#088;font-weight:bold">const</span> string lookingFor = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">hello</span><span style="color:#710">"</span></span>;
Parallel.For(<span style="color:#00D">0</span>, <span style="color:#0a5;font-weight:bold">int</span>.MaxValue, i =>
{
var rnd = new Random(i);
<span style="color:#080;font-weight:bold">if</span> (CompareToRandomString(lookingFor, rnd))
Console.WriteLine(i);
});</pre></td>
</tr></table>
<p><br/>
I leave it up to you to implement a <a href="http://msdn.microsoft.com/en-us/library/dd460721.aspx">break</a> out of that loop.</p>
<p><a href="http://www.ruby-doc.org/core-1.9.2/Random.html">Ruby</a> does not use a LGC but a <a href="http://en.wikipedia.org/wiki/Mersenne_twister">Mersenne twister</a> instead. It's still only pseudorandom so there's no problem finding patterns you like and being able to repeat them.</p>
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a>
<a href="#n2" name="n2">2</a>
</pre></td>
<td class="code"><pre>srand(<span style="color:#00D">2570940381</span>)
<span style="color:#00D">9</span>.times { print <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">%d </span><span style="color:#710">"</span></span> % rand(<span style="color:#00D">10</span>) }</pre></td>
</tr></table>
<p>Outputs:<br/>
<code>1 2 3 4 5 6 7 8 9</code></p>
<table class="CodeRay"><tr>
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a>
<a href="#n2" name="n2">2</a>
<a href="#n3" name="n3">3</a>
</pre></td>
<td class="code"><pre>charset=(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">a</span><span style="color:#710">'</span></span>..<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">z</span><span style="color:#710">'</span></span>).to_a
srand(<span style="color:#00D">124931</span>)
puts (<span style="color:#00D">0</span>...<span style="color:#00D">4</span>).map{ charset.to_a[rand(charset.size)] }.join</pre></td>
</tr></table>
<p>Outputs:<br/>
<code>ruby</code></p>Ian Besturn:uuid:4bbbd9ed-f65f-480c-b7b5-65e725b8e7d22012-03-22T10:52:35+01:002012-03-22T22:55:39+01:00Comment on Inspired by randomness by Ian Best<p>It was really interesting how you found the seed number. I will definitely implement a break out of the loop. There won’t be any issues finding patterns we like and repeat them.</p>