 |
|
|
 |
|
|
 |
|
 |
I like small simple code very much! Especially for such artistic purpose like this. And even greater that the author gives a test suite.
However, there are several kinds of uses for pseudo random numbers. For instance Monte-Carlo simulation, noise generation, and cryptography.
Generally I would also expect an autocorrelatoin test. You take a series of generated values and autocorrelate them. You should get a peak at offset zero and more or less constant low level result at all other offsets. In this case you can use it for simulation.
To produce white noise you also have to show that the spectrum meets your requirements.
If you look at LFSR generators they pass the autocorrelation test, produce white noise and are great PRNGs frequently used for simulation and autio or even spread spectrum noise generation. However, in crypto applications they are most crackable! They have still been in use by Russians in the eightees and have been read using Sinclair ZX81 computers running a 4 MHz 8 bit CPU. Maybe they should have been read...
Anyway. I like the article and will digg into it more deeply. But be warned that quality tests for PRNGs should be application specific.
Learndy
-- Airspace V - international hangar flying! http://www.airspace-v.com/ggadgets for tools & toys
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thanks for your note. I agree that generators need to be tested for autocorrelation etc. The test I provided is more of a demo than a thorough test. I don't want to imply that the test included with the project is enough. I'm leaning on George Marsaglia having tested his algorithm with his DIEHARD suite of tests.
The test I included would probably catch an error in my implementation of Marsaglia's algorithm, but not a flaw in his algorithm itself.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi,
you wrote that the interval 0 <= u <= 2^32 gets transformed to (0, 1) . The transformation is done by multiplying with 1 / (2^32 + 1) . Although u can be 0 the output interval should be [0, 1) - so 0 included while 1 is excluded.
Kind regards Gü
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Thus the code line returning the result in the GetUnitform method has to be changed to
return (u + 1) * 2.328306435454494e-010; Note: the number slightly differs from the "old" number because (2^32 + 1) is just a little bit smaller than (2^32 + 2) -> the 2^32 part dominates.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I've revised the code and the article. The new version was posted 6 Oct 2008.
modified on Monday, October 6, 2008 3:23 PM
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hello Mr. Cook.
I like your article ! Do you have any knowledge on designing RNG algorithms ? I mean all the math theory etc... If you do I would like to see some theory, in this or in another article. Perhaps this site isn't the best place to write articles for math but I think some programmers interested in math may find this usefull.
Thanks.
kostas KEL
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
For an introduction to the mathematical theory, you might want to start with volume 2 of Donald Knuth's Art of Computer Programming, Seminumerical Algorithms. Also, you may be interested in another CodeProject article I wrote, "[^]Pitfalls in Random Number Generation
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
 |
you can just do this :
Random ran = new Random(); double d = ran.NextDouble();
and u will get a double between 0 and 1 then u can do what ever u want with it so why spnd time trying to create one ?
|
| Sign In·View Thread·PermaLink | 1.11/5 |
|
|
|
 |
|
 |
For the fun maybe? Beside that, it's probably better to turn to System.Security.Cryptography for production code.
|
| Sign In·View Thread·PermaLink | 4.29/5 |
|
|
|
 |
|
|
 |
|
 |
One reason for a custom generator is transparency. If you're trying to reproduce a problem and stepping through the code with a debugger, everything is right there in your code under your control.
Sometimes it's helpful to compare results of code written in different environments. If they all use their own library's RNG, the results aren't comparable. But, for example, you could use this code from unmanaged C++ and from C# and produce identical sequences if you start from identical seeds.
modified on Friday, April 11, 2008 10:08 PM
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
 |
If you want 1 random number fine but if you need hundreds or thousand that will be truly random then it's a problem. I am trying to write a statistics app and I can't seem to get random numbers.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
For one reason, the bug that was recently discovered in the random number generator used by Vista and everything that came before.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I'll stick with what's built-in.
This is one of many areas where I know Microsoft can do better than I can.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
PIEBALDconsult wrote: This is one of many areas where I know Microsoft can do better than I can
This may be true, but others can do significantly better. You probably don't need really random numbers, but if you do, the in-built functions like rand() are not up to the task.
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
That's fine for crypto work, but what about for statistical simulations (Monte Carlo etc), here you need very fast random number generation with good statistical properties. The inbuilt Random() function looks similar to rand() from c. I haven't made any serious use of random numbers in .NET, but it is possible that this simple generator could be useful.
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
This PRNG is only usable for monte carlo simulations. And i think it is better than rand() for this purpose, because it passes the DIEHARD tests, the build in rand() function don't!
Where are the problems in crypthography? 1. It is only 32Bit wide - no problem for a brute force attack, at least you need 128Bit, 160Bit or 256Bit is better. 2. It is guessable. If you have a number, you can guess the previous number because the algorithm has not the strength of an cryptography hash function like SHA or even MD5. If the output must be salted and hashed to masquerade, it looses it statistical distribution because of the hash function. In this case you can use a salted hash function to produce a PRN sequence like this:
salt = something real random number = 0; // Dosn't matter because of the salt! number = CryptoHash( number xor salt ) number = CryptoHash( number xor salt ) (replace CryptoHash with your prefered hash function like SHA)
Due to the fact that cryptographical hash functions are slow and often don't produce a perfect statistical distribution, it is not very good to use them for monte carlo simulations.
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
 |
I neither speak nor understand statistics or math all that well, but I am absolutely sure that there is no way to generate true random numbers using a computer, so one algorithm is just as good as the other and all that Monte Carlo and DIEHARD talk is just salt in the eyes. I can always invent my own algorithm and then invent a test that it will pass. In fact it's quite easy, but I will always know that the numbers it will generate will never be absolutely random, so what's the point of all this theory? 
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |