Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with Random and Threads in .NET
    text
    copied!<p>I'm having trouble with the <a href="http://msdn.microsoft.com/en-us/library/system.random.aspx" rel="nofollow noreferrer">Random</a> class in .NET, I'm implementing a threaded collection which is working fine, except for one smaller detail. The collection is a <a href="http://en.wikipedia.org/wiki/Skip_list" rel="nofollow noreferrer">Skip list</a> and those of you familiar with it know that for every node inserted I need to generate a new height that is <code>&lt;= CurrentMaxHeight+1</code>, here's the code that I'm using to do that (I know it's very inefficient, but it works and thats my main priority now)</p> <pre><code>int randomLevel() { int height = 1; while(rnd.NextDouble() &gt;= 0.5 &amp;&amp; height &lt; MaxHeight) ++height; return height; } </code></pre> <p>My problem here is that sometimes I keep getting only 1 back from this for several thousand elements in a row which kills the performance of the skip list. The chance for 10.000 elements to generate only 1 from this method in a row, seems very slim (happens pretty consistently).</p> <p>So I'm assuming <em>(guessing)</em> that there is a problem with the <code>Random</code> object in some way, but I don't really know where to start digging around. So I turn to stackoverflow to see if anyone has an idea?</p> <p><strong>Edit</strong></p> <p>The rnd-variable is declared in the class <code>SkipList&lt;T&gt;</code>, and it is accessed from several threads (each thread calls .Add on the collection and Add calls .randomLevel)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload