English 中文(简体)
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?

最佳回答

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!

问题回答

Your stripy point-plotting problem should easily be fixed by generating a new random number for each of the x and y coordinates. Trying to reuse a single generated number for x and y is basically premature optimization, but if you do go down that route, make sure you extract different bits for each from the number; as is, x=n%width;y=n%height gives you enormous correlation between x and y, as can be seen in your images.

I ve been using various C++ Mersenne Twister implementations for years (most recently boost s) to generate random points and had no difficulties with it (seed related or otherwise). It really is a superb generator.

True random number generation cannot be done with a mathematical function. If it s important to have truly random numbers, get a hardware random number generator. I ve developed real money online poker games—such hardware is the only way to be confident there are no patterns in the numbers.

If targeting a Linux environment, the /dev/random and /dev/urandom pseudo devices do a lot better than a mathematical generator, since they incorporate random numbers representing hardware activity.





相关问题
Weighted random numbers

I m trying to implement a weighted random numbers. I m currently just banging my head against the wall and cannot figure this out. In my project (Hold em hand-ranges, subjective all-in equity ...

Comprehensive information about hash salts

There are a lot of questions about salts and best practices, however most of them simply answer very specific questions about them. I have several questions which feed into one another. Assuming a ...

Generate unique names?

I am working on a php site in which we have to upload images from users.i have to rename that file for preventing conflicts in the name of the image. uniqid(rand(), true); and adding a large random ...

How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

热门标签