6 Replies - 5605 Views - Last Post: 20 January 2012 - 03:49 AM Rate Topic: -----

#1 blogwhodle   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 18-January 12

How does the Random Class in .NET work?

Posted 19 January 2012 - 08:09 PM

My question says it.
How does it work???
Or how does Donald E. Knuth's subtractive random number generator algorithm work? (Since that's how the random number class works)
:hammer:
Is This A Good Question/Topic? 0
  • +

Replies To: How does the Random Class in .NET work?

#2 darek9576   User is offline

  • D.I.C Lover

Reputation: 203
  • View blog
  • Posts: 1,731
  • Joined: 13-March 10

Re: How does the Random Class in .NET work?

Posted 19 January 2012 - 08:16 PM

Have you even made a slightest attempt to find it on the internet? Seriously, get a grip.
Was This Post Helpful? 0
  • +
  • -

#3 blogwhodle   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 18-January 12

Re: How does the Random Class in .NET work?

Posted 19 January 2012 - 08:19 PM

View Postdarek9576, on 19 January 2012 - 08:16 PM, said:

Have you even made a slightest attempt to find it on the internet? Seriously, get a grip.

I sure have okay. :yes:
And I came here because I couldn't find anything proper.
Was This Post Helpful? 0
  • +
  • -

#4 darek9576   User is offline

  • D.I.C Lover

Reputation: 203
  • View blog
  • Posts: 1,731
  • Joined: 13-March 10

Re: How does the Random Class in .NET work?

Posted 19 January 2012 - 08:25 PM

There is NOTHING on the internet about Random class in .NET? Are you kidding me????????????
Was This Post Helpful? 1
  • +
  • -

#5 blogwhodle   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 18-January 12

Re: How does the Random Class in .NET work?

Posted 19 January 2012 - 08:37 PM

There are many things. But noone says how it works.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How does the Random Class in .NET work?

Posted 19 January 2012 - 11:18 PM

MSDN PAGE said:

For more information, see D. E. Knuth. "The Art of Computer Programming, volume 2: Seminumerical Algorithms". Addison-Wesley, Reading, MA, second edition, 1981.


The book was written in 1981. There is a good chance it is in your public library system.

Amazon.com has it
http://www.amazon.co...27039499&sr=8-1

Wikipedia has a good write up
http://en.wikipedia....ter_Programming

Thanks to its age it seems to be a public domain free ebook
http://ebooksfreedow...
And
http://saba.kntu.ac....%20-%20Addi.pdf

A couple google searches starting from the MSDN PAGE for the class in question seems to have gotten plenty of information. I think this is why Darek {justifiably} scoffed about you not finding anything. Sometimes you just need to dig a little.
Was This Post Helpful? 1
  • +
  • -

#7 janne_panne   User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: How does the Random Class in .NET work?

Posted 20 January 2012 - 03:49 AM

You can also decompile the class to see how it is implemented. Here is Random class decompiled with ILSpy:

using System;
using System.Runtime;
using System.Runtime.InteropServices;
namespace System
{
	/// <summary>Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet certain statistical requirements for randomness.</summary>
	/// <filterpriority>1</filterpriority>
	[ComVisible(true)]
	[Serializable]
	public class Random
	{
		private const int MBIG = 2147483647;
		private const int MSEED = 161803398;
		private const int MZ = 0;
		private int inext;
		private int inextp;
		private int[] SeedArray = new int[56];
		/// <summary>Initializes a new instance of the <see cref="T:System.Random" /> class, using a time-dependent default seed value.</summary>
		public Random() : this(Environment.TickCount)
		{
		}
		/// <summary>Initializes a new instance of the <see cref="T:System.Random" /> class, using the specified seed value.</summary>
		/// <param name="Seed">A number used to calculate a starting value for the pseudo-random number sequence. If a negative number is specified, the absolute value of the number is used. </param>
		public Random(int Seed)
		{
			int num = (Seed == -2147483648) ? 2147483647 : Math.Abs(Seed);
			int num2 = 161803398 - num;
			this.SeedArray[55] = num2;
			int num3 = 1;
			for (int i = 1; i < 55; i++)
			{
				int num4 = 21 * i % 55;
				this.SeedArray[num4] = num3;
				num3 = num2 - num3;
				if (num3 < 0)
				{
					num3 += 2147483647;
				}
				num2 = this.SeedArray[num4];
			}
			for (int j = 1; j < 5; j++)
			{
				for (int k = 1; k < 56; k++)
				{
					this.SeedArray[k] -= this.SeedArray[1 + (k + 30) % 55];
					if (this.SeedArray[k] < 0)
					{
						this.SeedArray[k] += 2147483647;
					}
				}
			}
			this.inext = 0;
			this.inextp = 21;
			Seed = 1;
		}
		/// <summary>Returns a random number between 0.0 and 1.0.</summary>
		/// <returns>A double-precision floating point number greater than or equal to 0.0, and less than 1.0.</returns>
		[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
		protected virtual double Sample()
		{
			return (double)this.InternalSample() * 4.6566128752457969E-10;
		}
		private int InternalSample()
		{
			int num = this.inext;
			int num2 = this.inextp;
			if (++num >= 56)
			{
				num = 1;
			}
			if (++num2 >= 56)
			{
				num2 = 1;
			}
			int num3 = this.SeedArray[num] - this.SeedArray[num2];
			if (num3 == 2147483647)
			{
				num3--;
			}
			if (num3 < 0)
			{
				num3 += 2147483647;
			}
			this.SeedArray[num] = num3;
			this.inext = num;
			this.inextp = num2;
			return num3;
		}
		/// <summary>Returns a nonnegative random number.</summary>
		/// <returns>A 32-bit signed integer greater than or equal to zero and less than <see cref="F:System.Int32.MaxValue" />.</>/returns>
		/// <filterpriority>1</filterpriority>
		[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
		public virtual int Next()
		{
			return this.InternalSample();
		}
		private double GetSampleForLargeRange()
		{
			int num = this.InternalSample();
			bool flag = this.InternalSample() % 2 == 0;
			if (flag)
			{
				num = -num;
			}
			double num2 = (double)num;
			num2 += 2147483646.0;
			return num2 / 4294967293.0;
		}
		/// <summary>Returns a random number within a specified range.</summary>
		/// <returns>A 32-bit signed integer greater than or equal to <paramref name="minValue" /> and less than <paramref name="maxValue" />; that is, the range of return values includes <paramref name="minValue" /> but not <paramref name="maxValue" />. If <paramref name="minValue" /> equals <paramref name="maxValue" />, <paramref name="minValue" /> is returned.</returns>
		/// <param name="minValue">The inclusive lower bound of the random number returned. </param>
		/// <param name="maxValue">The exclusive upper bound of the random number returned. <paramref name="maxValue" /> must be greater than or equal to <paramref name="minValue" />. </param>
		/// <exception cref="T:System.ArgumentOutOfRangeException">
		///   <paramref name="minValue" /> is greater than <paramref name="maxValue" />. </exception>
		/// <filterpriority>1</filterpriority>
		public virtual int Next(int minValue, int maxValue)
		{
			if (minValue > maxValue)
			{
				throw new ArgumentOutOfRangeException("minValue", Environment.GetResourceString("Argument_MinMaxValue", new object[]
				{
					"minValue", 
					"maxValue"
				}));
			}
			long num = (long)maxValue - (long)minValue;
			if (num <= 2147483647L)
			{
				return (int)(this.Sample() * (double)num) + minValue;
			}
			return (int)((long)(this.GetSampleForLargeRange() * (double)num) + (long)minValue);
		}
		/// <summary>Returns a nonnegative random number less than the specified maximum.</summary>
		/// <returns>A 32-bit signed integer greater than or equal to zero, and less than <paramref name="maxValue" />; that is, the range of return values ordinarily includes zero but not <paramref name="maxValue" />. However, if <paramref name="maxValue" /> equals zero, <paramref name="maxValue" /> is returned.</returns>
		/// <param name="maxValue">The exclusive upper bound of the random number to be generated. <paramref name="maxValue" /> must be greater than or equal to zero. </param>
		/// <exception cref="T:System.ArgumentOutOfRangeException">
		///   <paramref name="maxValue" /> is less than zero. </exception>
		/// <filterpriority>1</filterpriority>
		public virtual int Next(int maxValue)
		{
			if (maxValue < 0)
			{
				throw new ArgumentOutOfRangeException("maxValue", Environment.GetResourceString("ArgumentOutOfRange_MustBePositive", new object[]
				{
					"maxValue"
				}));
			}
			return (int)(this.Sample() * (double)maxValue);
		}
		/// <summary>Returns a random number between 0.0 and 1.0.</summary>
		/// <returns>A double-precision floating point number greater than or equal to 0.0, and less than 1.0.</returns>
		/// <filterpriority>1</filterpriority>
		[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
		public virtual double NextDouble()
		{
			return this.Sample();
		}
		/// <summary>Fills the elements of a specified array of bytes with random numbers.</summary>
		/// <param name="buffer">An array of bytes to contain random numbers. </param>
		/// <exception cref="T:System.ArgumentNullException">
		///   <paramref name="buffer" /> is null. </exception>
		/// <filterpriority>1</filterpriority>
		public virtual void NextBytes(byte[] buffer)
		{
			if (buffer == null)
			{
				throw new ArgumentNullException("buffer");
			}
			for (int i = 0; i < buffer.Length; i++)
			{
				buffer[i] = (byte)(this.InternalSample() % 256);
			}
		}
	}
}


Was This Post Helpful? 4
  • +
  • -

Page 1 of 1