Search code examples
randomvisualizationseedingmersenne-twister

Mersenne Twister: seeding & visualization


I am using a C# implementation of Mersenne Twister I downloaded from CenterSpace. I have two problems with it:

  1. No matter how I seed the algorithm it does not pass DieHard tests, and by that I mean I get quite a lot of 1s and 0s for p-value. Also my KStest on 269 p-values is 0. Well, I cannot quite interpret p-value, but I think a few 1s and 0s in the result is bad news.
  2. I have been asked to visually show the randomness of the numbers. So I plot the numbers as they are generated, and this does not seem random at all. Here is two screenshots of the result after a few seconds and a few seconds later. As you can see in the second screenshot the numbers fall on some parallel lines. I have tried different algorithms to map numbers to points. They all result in parallel lines, but with different angles! This is how I mapped numbers to points for these screenshots: new Point(number % _canvasWidth, number % _canvasHeight). As you may guess, the visual result depends on the form's width and height, and this is a disasterous result.

Here is a few ways I tried to seed the algorithm:

  1. User entry. I enter some numbers to seed the algorithm as an int array.
  2. Random numbers generated by the algorithm itself!!
  3. An array of new Guid().GetHashCode()

What am I missing here? How should I seed the algorithm? How can I get it pass the DieHard?


Solution

  • While I cannot speak to your first point, the second problem has to do with how you are computing the points to draw on. Specifically,

    x = number % _canvasWidth;
    y = number % _canvasHeight;
    

    will give you a "pattern" that corresponds somewhat to the aspect ratio of the window you are drawing to. For example, if _canvasWidth and _canvasHeight were equal, you would always draw on a single diagonal line as x and y would always be the same. This graphical representation wouldn't be appropriate in this case, then.

    What about taking the N bits of the RNG output and using half for the x coordinate and the other half for the y coordinate? For those bits that fall out of the bounds of your window you might want to consider two options:

    1. Don't draw them (or draw them offscreen)
    2. Perform a linear interpolation to map the range of bits to the width/height of your window

    Either option should give you a more representative picture of the bits you are getting our of your random number generator. Good luck!