I was wondering how the Random functions in every programming language works so I want to generate a number by myself i.e. I don't want to use any inbuilt classes or functions.
-
4"I don't want to use any inbuilt classes or functions." Why not? And see [here](http://en.wikipedia.org/wiki/Random_number_generation) for how a RNG works. You really want to rely on framework functionality here. – CodeCaster Sep 24 '11 at 16:28
-
See http://en.wikipedia.org/wiki/Random_number_generation – H H Sep 24 '11 at 16:28
4 Answers
If you are curious how it works you can start with Wikipedia: Random number generation and List of random number generators. The second link will give you list of few popular algorithms (like Mersenne Twister) that you can implement by yourself.
You can also decompile System.Random with .Net Reflector and compare given algorithms with what is implemented natively in .Net
Also, Art of Computer Programming by D. Knuth has a chapter on random numbers and their generation.

- 11,789
- 2
- 33
- 41
-
Is the BCL random support actually implemented in .Net i.e MSIL? I would suspect it is done natively. – Tim Lloyd Sep 24 '11 at 16:47
-
Yes it is implemented in .Net. They're generating 1 array (for each Random object) with around 50+ elements and fill it with something similar to MWC I think and then returning differences between 2 fields in this seed-array while also updating it's fields, so it's pretty fast (just few accesses to managed array). – Marcin Deptuła Sep 24 '11 at 18:06
-
1@chibacity _"The current implementation of the Random class is based on a modified version of Donald E. Knuth's subtractive random number generator algorithm."_ and `RNGCryptoServiceProvider` is using the native [`CryptGenRandom`](http://msdn.microsoft.com/library/aa379942.aspx) function. – ordag Sep 24 '11 at 18:40
For simplicity and speed, it's hard to beat the Xorshift random number generator. Whether it generates a good distribution is another question.
One example in C#: http://www.codeproject.com/KB/cs/fastrandom.aspx
Different languages and environments use different random number generators. As others have pointed out, there are lots of ways to generate pseudo-random numbers.
See C# Normal Random Number and other similar Stack Overflow questions.

- 1
- 1

- 131,090
- 20
- 188
- 351
-
1*"Whether it generates a good distribution is another question"* That's only the case if it's poorly seeded. Xorshift will produce some of the most beautiful noise you'll ever see, so long as you're hashing the seeds during initialization. Here's a great writeup comparing LCG vs Xorshift + Wang hash. http://www.reedbeta.com/blog/2013/01/12/quick-and-easy-gpu-random-numbers-in-d3d11/ – arkon Aug 07 '16 at 03:18
As someone else commented, you really want to rely on framework functionality for this. If this is for academic purposes or out of pure interest, there are a number of RNG algorithms that are easy to implement. One is the multiply-with-carry (MWC) algorithm, which can be easily implemented in C#:
public class RNG
{
// Seeds
static uint m_w = 362436069; /* must not be zero */
static uint m_z = 521288629; /* must not be zero */
public int NextRandom()
{
m_z = 36969 * (m_z & 65535) + (m_z >> 16);
m_w = 18000 * (m_w & 65535) + (m_w >> 16);
return (int)((m_z << 16) + m_w);
}
}
For details on MWC, see http://www.bobwheeler.com/statistics/Password/MarsagliaPost.txt

- 161,458
- 45
- 265
- 341
To Generate the Random number without using Random() method
using System;
public class GenerateRandom
{
private int max;
private int last;
static void Main(string[] args)
{
GenerateRandom rand = new GenerateRandom(10);
for (int i = 0; i < 25; i++)
{
Console.WriteLine(rand.nextInt());
}
}
// constructor that takes the max int
public GenerateRandom(int max)
{
this.max = max;
DateTime dt = DateTime.Now;//getting current DataTime
int ms = dt.Millisecond;//getting date in millisecond
last = (int) (ms % max);
}
// Note that the result can not be bigger then 32749
public int nextInt()
{
last = (last * 32719 + 3) % 32749;//this value is set as per our requirement(i.e, these is not a static value
return last % max;
}
}

- 757
- 8
- 9