2

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.

2
  • 4
    "I don't want to use any inbuilt classes or functions." Why not? And see here for how a RNG works. You really want to rely on framework functionality here. Commented Sep 24, 2011 at 16:28
  • See en.wikipedia.org/wiki/Random_number_generation Commented Sep 24, 2011 at 16:28

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

"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. reedbeta.com/blog/2013/01/12/…
3

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.

3 Comments

Is the BCL random support actually implemented in .Net i.e MSIL? I would suspect it is done natively.
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).
@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 function.
2

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

Comments

1

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;
    }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.