|
Author Topic:   Fast Random Numbers
Kaeto
Member
posted August 28, 1999 11:44 AM            
Anyone got any ideas as to how to generate some pseudorands VERY quickly? rand() is a slow as heck. And for what I'm doing patterns show up nasty. Some kind of two vars used together system?

IP:

LDA Seumas
unregistered
posted August 28, 1999 12:15 PM           
rand() is likely using a modulo operation, which is basically a divide, which is terribly slow if you're doing it a lot.

Table based random number generators can be very fast, and very random. It doesn't matter how long it takes you to make your table, and you can make it as random as computerly possible. Generally, if you XOR multiple random numbers together that are generated by different psuedorandom algorithms, your result will be more random than either of the sources by themselves.

If you just want something that's faster than rand(), an algorithm that just uses a multiply, shifts, and bit operations may be faster. But if you're looking for more randomness as well, you might have to go further.

The book Inner Loops by Rick Booth includes a few pseudorandom algorithms, in addition to lots of dirt on Pentium instruction speeds, and hashing and searching algorithms.

------------------
-- Seumas McNally, Lead Programmer, Longbow Digital Arts

IP:

Kaeto
Member
posted August 28, 1999 12:38 PM            
Thanks. I've know for a long time that rand is as slow (I've written a TON of crappy little Dos programs ) and i've seen all kinds of implementations, but I just wanted to know what might be the fastest way to generate them. (I guess I really don't need them _that_ random, but it would be nice )

IP:

LDA Seumas
unregistered
posted August 28, 1999 12:54 PM           
Here are the two simplest random generators from the book:

random_num = (Seed = (Seed * 1664525) + 1013904223);

and:

random_num = ((seed = seed * 1103515245 + 12345) >> 16) & 0x7fff;

------------------
-- Seumas McNally, Lead Programmer, Longbow Digital Arts

IP:

Kaeto
Member
posted August 28, 1999 03:38 PM            
Thanks. I made a modified backbuffer that lets you turn the Dot Creation on and off, and has sparkling implemented as an option too. I made the burning use a table-based random number generator... let the user change the burn rate in real time (I regen the table. Only takes a fraction of a second) I could email anybody whose interested for whatever reason the backbuffer2.cpp file. It's still pretty simple and I put in a few little explanations. Can't stop hitting alt-f-l (Save all in Visual C++) just did it in Netscape. DOH!

IP:

Kaeto
Member
posted August 28, 1999 03:39 PM            
Thanks. I made a modified backbuffer that lets you turn the dot creation on and off, and has sparkling implemented as an option too. I made the burning use a table-based random number generator... let the user change the burn rate in real time (I regen the table. Only takes a fraction of a second) I could email anybody whose interested for whatever reason the backbuffer2.cpp file. It's still pretty simple and I put in a few little explanations. I just used rand() for this but I always have had problems with random numbers and wanted the info. Thanks again.

IP: