Internal details of System.Random

R

rossum

I am looking for a source for the internal details of the
System.Random pseudo random number generator. Something reasonably
technical please, not how to use it (which I know) but more how does
it work under the hood.

Failing that I have three specific questions:

1 Is the core PRNG using integer arithmetic or floating point
arithmetic? The presence of Sample() makes me suspect that it is
floating point.

2 Is the protected method Sample() faster, slower or about the same as
the public method NextDouble()?

3 Is there anything special about Sample() compared to the way
NextDouble() operates?

For amusement I am building a PRNG class derived from System.Random so
it is useful to know a bit of detail about the internals of the base
class so I can make the best use of it.

Thanks in advance,

rossum
 
R

rossum

If you want to achive just better performance use this one:
http://www.codeproject.com/csharp/FastRandom.asp

Thanks for the link Milosz. Although I probably won't use the
FastRandom generator, the performance table and some of the text on
that page was very helpful. The link says:

"System.Random is actually very fast and achieves its speed mostly by
only using simple and fast arithmetic operations such as shift and
add. However, the whole class is based around a central Sample()
method that returns a double between 0.0 and 1.0, and thus there is
some unnecessary floating point arithmetic used to generate integer
values."

The timing for NextDouble() shows it is about twice as fast as
NextInt(). This probably measn that as I suspected, System.Random is
based on a floating point PRNG with Sample() as the basic generator.

rossum
 
G

Guest

Hi again,

You aked about implementation details of the System.Random class. I'm not
sure about .NET 1.0/1.1 but .NET 2.0 uses implementation of the Random class
based on Donald E. Knuth's subtractive random number generator algorithm.
(see D. E. Knuth. "The Art of Computer Programming, volume 2: Seminumerical
Algorithms". Addision-Wesley, Reading, MA, second edition, 1981.). I suspect
previous version of .NET uses this implementation as well.

Hope this helps
 
R

rossum

Hi again,

You aked about implementation details of the System.Random class. I'm not
sure about .NET 1.0/1.1 but .NET 2.0 uses implementation of the Random class
based on Donald E. Knuth's subtractive random number generator algorithm.
(see D. E. Knuth. "The Art of Computer Programming, volume 2: Seminumerical
Algorithms". Addision-Wesley, Reading, MA, second edition, 1981.). I suspect
previous version of .NET uses this implementation as well.

Hope this helps


Thanks again, that is useful. I have a copy of Knuth so I shall look
it up.

rossum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top