0

I need to create a web services receiving some data and returning a Random Number. The Random Number has to be generated by Mersenne Twister Algorithm.

One of the specification is seeding the Mersenne Twister. Because is a web services do I have to seed the Mersenne object in every request with a new seed?

Mike
  • 173
  • 2
  • 5

2 Answers2

0

Like every pseudo random generator the list of outputs generated are created by using the said seed. If you give the same seed the generated sequence output will always be the same. So you should ask if you want a constant sequence or a truly random one.

To answer your question:

You could use a static constant value in the WebService (if security is a concern I wouldn't recommend this) or you could use something like:

long next_seed = DateTime.Now.Ticks ^ someValue;

to generate a "random" seed.

Hope this helps

  • Why security would be an issue if I use static ? – Mike Nov 19 '15 at 22:01
  • Because if you retrieve some fixed output based on a fixed input and fixed seed then someone with enough time can figure that out... Unless the service you are providing doesn't need to be secure you could ignore that aspect. In the end it's your call, I was just pointing some general cases. Another thing I was refering to a static constant not a static variable because that can be randomized for each call. – MrCOPYPASTE Nov 19 '15 at 22:30
  • Since it's a web service, the class is `new`ed up on each request, so he wouldn't necessarily have to provide a seed value to keep it random. He/she could just initialize the PRNG class and let it use the default `DateTime.Now.Ticks` since it wouldn't be easily guessable. Personally, however, I wouldn't do that, I would write a class so I have full control of the seeding process. – Derreck Dean Nov 20 '15 at 18:01
0

It's a web service, so the class would be initializing from scratch on every call and be reseeded anyway, unless you make the Mersenne Twister randomizer class instance static so it doesn't need to be reseeded. If you do that, however, you should reinitialize (destroy/recreate) the instance every so often, as the Mersenne Twister values are guessable after 600-something pulls.

For performance, it would be best to make a static instance and reinitialize every 500 pulls as per the following pseudocode:

class RandomNumberGenerator
{

    static MersenneTwisterClass RNG = null;
    static object RNGLock = new object();
    static int counter = 0;

    public RandomNumberGenerator()
    {
        Init();
    }

    void Init()
    {
        // Multithreading lock
        lock (RNGLock)
        {
            // Seed it or don't, your call
            RNG = new MersenneTwisterClass(some_seed_value_or_nah);
            counter = 0;
        }
    }

    public decimal GetValue()
    {
        lock (RNGLock)
        {
            counter++;
            if (counter > 500)
            {
                Init();
            }
            return RNG.GetValue();
        }
    }

    public long GetRange(long min, long max)
    {
        lock (RNGLock)
        {
            counter++;
            if (counter > 500)
            {
                Init();
            }
            return RNG.GetRange(min, max);
        }
    }

}
Derreck Dean
  • 3,708
  • 1
  • 26
  • 45