Random number generation efficiency?
by Jake Seigel · in Torque Game Builder · 03/26/2008 (5:58 am) · 5 replies
Hey there, does anyone know the efficiency for generating random numbers with script? As in, is it a drain on time if you're using it too often? Is it worth it to recalculate a list of numbers at load time instead of generating them in real time on the fly?
About the author
#2
In terms of coding the algorithm in script vs source. Source is the better way to go. That said, code it in script first because it's easier to debug and see if it has an impact on the game. If you can't notice it's randomly generating numbers in script, ie. no big delays, keep it in script. You can always recode that sort of thing in source later.
A caveat in random number generators, you need a good algorithm. Otherwise, your numbers won't be very random.
03/26/2008 (7:59 am)
Loading time can already be fairly long if you're loading a lot of images, sound, large levels. It might be more efficient to calculate a random number during the game. It might be one thirtieth of a second throughout the game rather than two whole seconds at the beginning. Ideally the game loads in under two seconds so the player can jump right into it.In terms of coding the algorithm in script vs source. Source is the better way to go. That said, code it in script first because it's easier to debug and see if it has an impact on the game. If you can't notice it's randomly generating numbers in script, ie. no big delays, keep it in script. You can always recode that sort of thing in source later.
A caveat in random number generators, you need a good algorithm. Otherwise, your numbers won't be very random.
#3
for example, it's way more expensive than taking a sine or cosine, but less expensive than doing a raycast.
but computers are very fast, so you're probably fine.
if you're calculating fewer than say five hundred random numbers per second
and you're using a built-in random number generator, i think you don't need to start worrying about this.
03/26/2008 (10:29 am)
Generating random numbers is very expensive, relatively speaking.for example, it's way more expensive than taking a sine or cosine, but less expensive than doing a raycast.
but computers are very fast, so you're probably fine.
if you're calculating fewer than say five hundred random numbers per second
and you're using a built-in random number generator, i think you don't need to start worrying about this.
#4
work to generate each new number. About 15-20 lines of bit mangling in one version I've
adapted into a C++ class. Fast enough to not strain an ARM9 :)=
03/26/2008 (1:56 pm)
Look up the Mersenne Twister PRNG. It's generally considered the best, and it isn't all that muchwork to generate each new number. About 15-20 lines of bit mangling in one version I've
adapted into a C++ class. Fast enough to not strain an ARM9 :)=
#5
I didn't mean making my own code for generation. As Orion said i was more worried about performing the random number generation as many as 10 times at once in the middle of an action game. My game atm needs to be optimized so i can't really tell if that's causing a slowdown or not. I didn't assume that there would be issues, but i was wondering if there was something i should know to avoid. If not, that's great.
03/26/2008 (6:02 pm)
Hey there, sorry i couldn't reply to this earlier; i've been kicked off of the GG site all day. I didn't mean making my own code for generation. As Orion said i was more worried about performing the random number generation as many as 10 times at once in the middle of an action game. My game atm needs to be optimized so i can't really tell if that's causing a slowdown or not. I didn't assume that there would be issues, but i was wondering if there was something i should know to avoid. If not, that's great.
Torque Owner Derelict
If that is the case, I'd suggest it isn't a very efficient way to go. So far as I understand, the existing random number generator is very robust insofar as the randomness goes. Given that it is also a product of the source (i.e., C++), it should be very efficient in terms of performance.
Otherwise, if you mean to use TorqueScript alongside the existing random number generator, I can't imagine that front-loading the generation of numbers would provide much of an performance advantage over doing so "on the fly". Chances are, your time would be better spent investigating and coding other things. :)