Sponsored by Activision
Introducing DJ Hero Game view!
djhero.com - Scratch and mix 102 songs in 93 original mixes from today's hottest artists. Available Now.
54 Comments
- kruykaze, on 10/12/2007, -0/+11If anybody needs a random number contact me.I'll give you your first number for free!!
- robbyjo, on 10/12/2007, -0/+9Everyone here seems to be clueless. The point of Mersenne Twister or R250/521 is NOT about faster implementation of random variable! It's about MORE randomness because of MORE period. Why? Because even the ISO standard (or /dev/random or what have you) has a puny 4 billion period, which is probably enough for the majority of people. HOWEVER, for scientific purposes, it is NOT enough, especially for simulations. Simulations often requires hundred of billions of random variables. If the random generator repeats itself before the simulation finishes, it will skew the result. And that's bad.
These two implementations on the webpage are the fastest implementations of the two algorithms and still comparable in speed.
I've personally use Mersenne Twister for scientific purposes and it's damn good. I personally use Sean Luke's implementation in Java: http://www.cs.umd.edu/~seanl/gp/mersenne/MersenneTwister.java
This one is more drop-off replacement of Java's Random class, which is more convenient to use. - inactive, on 10/12/2007, -3/+12the article covers C, C# and Java
- Tezkah, on 10/12/2007, -1/+9Can we all agree to bury comments that paste advertisements into them?
edit: this was meant as a reply to "handeyman" up there. Oops, sorry. - inactive, on 10/12/2007, -13/+19Not really.
- azugaldia, on 10/12/2007, -0/+6The C# code simply does not work. The arrays are not defined using the C# syntax. Moreover, there is an int constant which exceeds its limit, and several implicit casts from long to int (you can check all of this just trying to compile the code with any C# compiler).
That's why I wonder if this code is really valuable. Has anybody seen how to send comments to the author? - FilCab, on 10/12/2007, -0/+5You mean "All pseuso-random number generators aren't 100% random".
You can always have a PCI card that generates real random numbers with a little help from quantum physics :-D - InternetUser, on 10/12/2007, -3/+8Bury? I block/report them.
- EiderDuck, on 10/12/2007, -0/+4I'm working on Monte Carlo simulation system now now and the default random number generator is one of the bottlenecks I'm trying to get around (yes I profiled).
- rockforever, on 10/12/2007, -0/+4It depends. If your program requires many random numbers than the generator quickly becomes a bottleneck.
In other words, there is something better out there in case you need it. - iczman, on 10/12/2007, -0/+4To be technical, all random generators aren't 100% random, but those algorithms in the article will give you non-repeating pattern of numbers for a long while before it starts to repeat again.
- PezFr33k, on 10/12/2007, -1/+4Faster is not always better. If you're using the random number for *anything* security related, you should opt for slower speeds for better "randomness".
openssl comes with a good secure PRNG...
http://www.openssl.org/docs/crypto/rand.html
I'm pretty sure there's a java port of this someplace as well. Essentially, it generates a hash from a random state plus current counter. - kodek, on 10/12/2007, -2/+5How is it that this algorithm is faster than java.util.Random when the constructor of one of the Java classes instantiates a java.util.Random object and then uses it?
EDIT:Nevermind. It's only used once. If one makes more than one call to the random() method, then this should be more efficient. - durk, on 10/12/2007, -0/+3In addition to that (and if worked syntactically), it won't return anything but the result of the built-in Rand function for the first 624 calls.
- zweben, on 10/12/2007, -1/+4We don't need to agree on it. It will be done.
- muyuu, on 10/12/2007, -0/+3EA has libraries for such things.
They put a lot of work on that, since they reuse it quite a lot. Many games, many platforms. I believe it's part of EA-math (closed source). - Cybert, on 10/12/2007, -0/+2No deterministic alogrithm will give you different videos (it doesn't matter how closed source it is). It has to pull some random information from somewhere.
- yahoofrom, on 10/12/2007, -0/+2my first number: 3729623
- drigz, on 10/12/2007, -0/+2The Python random implementation is written in Python - look at random.py in a python distribution. It seems they translated it from C:
# Translated by Guido van Rossum from C source provided by
# Adrian Baddeley. Adapted by Raymond Hettinger for use with
# the Mersenne Twister and os.urandom() core generators.
probably for reliability and portability...
The author's point was that, while it is available in some major languages (the kickass ones like Python :p) many others lack it. - andrewjw, on 10/12/2007, -2/+3Is the default random number function a bottleneck in applications? I think there are lots of other areas for optimization and random number optimization is not a pirority.
- cyc66, on 10/12/2007, -0/+1It can be. For instance, you might need to generate 5000 random numbers for a shuffling algorithm on a cell phone game. Depending on the phone this can take a long time and if you have an algorithm that's even twice as fast that's a huge savings.
- Cybert, on 10/12/2007, -0/+1If you're using library code, be careful. Linear Congruential Generators (the lazy default implementation) are very unrandom in the lower bits. I'd like hardware random numbers to be used, which can be stored for replays.
- boran, on 10/12/2007, -0/+1The point is that having a weak random number generator is a security weakness. Also random numbers are used very often in games so having high performance there matters.
- jeblis, on 10/12/2007, -0/+1Ahh admitting ignorance.
Nothing in a computer program is truly random. Random number generators (or more correctly pseudo-random number generators) work by taking a seed value and producing what appears to be a random or evenly distributed set of numbers. If you give it the same seed number it will produce the same set of random numbers. So when you measure the quality of a random number algorithm you look at speed of the algorithm and how well the numbers returned by it approximate a random distribution.
In order to be random the seed must be taken from something external to the computer algorithm. Seeds used are typically based upon the time, time between keystrokes, mouse movements etc. - bytemynd, on 10/12/2007, -0/+1The code may be his, but the algorithms were very indepthly researched and tested.
Just do a little googlin'. - Cybert, on 10/12/2007, -1/+2Hey any console people here? Always wondered if they use hardware random numbers. I think the XBOX does, as it gave a different video in Tiger Woods with the same default date and time.
- Cybert, on 10/12/2007, -0/+1Yes, it's about the quality not the speed. An LCG is pretty fast, but horrible for quality. Low order bits can be quite bad especially.
- muyuu, on 10/12/2007, -0/+1The hardware clock is usually a good seed, especially at millisecond precision. This trick is older than the cowbell video.
- drigz, on 10/12/2007, -0/+1Using C to generate 1,000,000,000 random numbers, his takes 1.6 seconds, rand() takes 34.5 seconds. No contest there... try upping the number you generate.
- handeyman, on 10/12/2007, -0/+1haha suck my dick Tezkah you know how much traffic I get from posting my link in comments
- Cybert, on 10/12/2007, -0/+1So Java uses a linear congruential generator? Anyone take a look at the source as I don't have it at the moment? Well that's certainly quick but not very "random" at all. I've seen something called ISAAC too.
- Dralex75, on 10/12/2007, -0/+1If you want the MT algo, go to the main site
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/eversions.html
It has implementations for several languages and better initialization code.
Don't use MT, use mt19937ar. - buckyboy314, on 10/12/2007, -1/+1I block/report them too, but that means they'll be there for people who haven't yet.
- Sartori, on 10/12/2007, -2/+2Games programmers are happy with predictably random numbers (ie starting with the same seed we always get the same set of random numbers coming back), if our game needs to be fully deterministic and support replays that is.
Whether these numbers are "good" random or not, I'm not sure, I'm just a gameplay programmer. I use the random number generators the more mathematically minded coders write :) - Slagar, on 10/12/2007, -3/+3It's not so much that it's a bottleneck rather that is isn't a true random number generator. Even with the limited things I've done the basic psuedo random number generators pop up time and time again and are always a pain in the butt to do anything with.
- inactive, on 10/12/2007, -3/+3 public void TestMT() {
for(int i = 0; i < 3; i ) {
MT random = new MT();
DateTime start = DateTime.Now;
for(int j = 0; j < 100000000; j ) {
random.Random();
}
DateTime end = DateTime.Now;
Console.Error.WriteLine("Time: " (end - start));
}
}
public void TestSystemRandom() {
for(int i = 0; i < 3; i ) {
Random random = new Random();
DateTime start = DateTime.Now;
for(int j = 0; j < 100000000; j ) {
random.NextDouble();
}
DateTime end = DateTime.Now;
Console.Error.WriteLine("Time: " (end - start));
}
}
Results:
MT:
Time: 00:00:04.1264256
Time: 00:00:04.1733168
Time: 00:00:04.1420560
System.Random:
Time: 00:00:03.0479280
Time: 00:00:03.0166672
Time: 00:00:03.0479280
Yes, System.Random seems faster than his MT implementation. However MT probably gives better PRND numbers. (C# 2.0) - FilCab, on 10/12/2007, -4/+4@Slagar: WTF? Do you even know what a pseudo-random number generator is? Do you know how one can have a TRUE random number generator using a PC? Google it...
FYI: The source code is for a PSEUDO-random number generator... - Slagar, on 10/12/2007, -1/+1@FilCab righto.. I realize that the article wasn't about real random numbers, just good fake ones. In my statement I was responding to the other fellows comment on why so much work is going into the random number generators, and the reason is, well, we don't have them. (and probably won't..)
- dood, on 10/12/2007, -0/+0The C R250/R521 programs call rand() multiple times -- I'm not sure how that would be faster than calling rand() once?
- DiggerT, on 10/12/2007, -3/+2hmm strange he complains about "java.util.Random" then proceeds to use it in both his java implementations of the alogrithms
- tnsimonson, on 10/12/2007, -2/+1"Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin."
-John von Neumann - rand(), on 10/12/2007, -1/+0Time for a coincidental, one-liner extravaDanza.
1.) So you're calling me slow and predictable? I guess I'll go back to slashdot...
2.) And all those programmers said "It's not you, it's me." I guess I know the truth now.
3.) Oh sure, a new, tight, 'fun' algorithm comes along, and I get kicked to the curb like so many lines of Perl.
4.) Kaaahhhnnn!
5.) Have I used ME? That makes me feel tingly in a recursive way...
6.) Sure, you can switch to a new random number generator, but every time you use it, you'll first type my name, and then have to backspace six times.
7.) When Chuck Norris was told the Mersenne Twister was not a variation on the Roundhouse Kick, he promptly roundhouse kicked Matsumora and Nishimura. It was logged by me in wikipedia as the most random thing to ever happen. Me, baby, ME. I win. I win one to nothin'. - prockcore, on 10/12/2007, -4/+3I use /dev/random
I don't know if it's good or not, but it's available regardless of the language I choose to use. - HMTKSteve, on 10/12/2007, -3/+1I used to use rand() in my program (http://www.hmtk.com) until someone offered me a better random number generator...
Now I use a better one for better randomness. Who was it that said, "random numbers are too important to leave to chance?" - phcreates, on 10/12/2007, -2/+0I know NOTHING about this, but I'm going to pretend I do. My comment is that either something is random or it is not, so 'more' or 'less' random is silly. Perhaps 'closer to random' or 'farther from random' would be appropriate?
- kalleanka, on 10/12/2007, -4/+2Omg, he writes a long article about how super fast his implementation is and then he did not even include a comparison with the built in functions.
This is just stupid. If you claim that your code is much faster, please _at least_ provide empirical tests proving your claims. - OrangeTide, on 10/12/2007, -3/+1"when applications that need good random numbers (like games) "
What? Games need good random numbers. It's just a game. Now *fast* random numbers I can see. I just use the terrible rand() code given in the ISO C spec!
static unsigned next_rand = 1;
unsigned rand(void) { next_rand=next_rand*1103515245L+12345; return (next_rand/65536)%32768; } - pkulak, on 10/12/2007, -4/+1I can come up with something slower and less random...
for (double i = 0; i < double.MaxValue; i++) {}
return 1; - FilCab, on 10/12/2007, -5/+2wtf are you trying to say?
These are all pseudo-random number generators, they repeat after a while... If you want true random number generators, go buy a PCI card that does that. - rupansensei, on 10/12/2007, -3/+0Wast this muppet on digg a few days ago for?
"Working at Microsoft"
http://www.qbrundage.com/michaelb/pubs/essays/working_at_microsoft.html
Is he a really clever boy or he just knows how to get onto the frontpage of digg? -
Show 51 - 54 of 54 discussions



What is Digg?