Game Development Community

Some words on Random()

by Jeremy Tilton · in Torque Game Builder · 03/12/2005 (6:30 pm) · 7 replies

Need some help with my math as well as how to round to nearest integer...

I want random to return even values between -22 and 22.

First, I'm assuming getRandom() returns a value between 0 and 1 like most Random Number Generators. So, I can get any value between -22 and 22 using:

-22 + (getRandom() * 44)

What utility in script rounds numbers to the nearest int.

Now how do I get only evens? Is there a modulus operator?

After all that is said and done, is "getRandom()" seeded? Can I feed it time to be "more random"? I get the same results every time I run it.

#1
03/12/2005 (6:36 pm)

I figured out how to get every even:

getRandom() between -11 and 11
Cast to nearest int
Multiply by 2

But I still don't know how to cast to nearest int.
#2
03/12/2005 (11:43 pm)
GetRandom() will return a float between 0 and 1
getRandom(10) will return an integer between 0 and 10
getRandom(-10,10) will return an integer between -10 and 10

% is the modulus operator

mFloor - round down to the nearest Integer
mCeil - round up to the nearest Integer
#3
03/13/2005 (8:19 am)
Beautiful, that is a much more functional random function! I was resorting to the old school formula I forgot after college:

([intervalsize]*(-1/4(i)+int(rand(1/2j))))

ew...

Any word on "Randomize Timer" or similar function? Or does it automatically do that? I know some random functions in some languages are automatically seeded with time, but not in all languages.
#4
03/13/2005 (10:42 am)
Actually it would be great to set the seed at will, since that way we could reproduce random numbers. A game recording system comes to mind.
#5
03/13/2005 (10:43 am)
@Jeremy: Hey! A question can sort of answer. I don't actually know if it is automatically seeding or not, but you can set the seed and get the seed with:

setRandomSeed([seed])
getRandomSeed([seed])

But it shouldn't take much to see if it is reproducible sequences. I know I saw a game time function for T2D, but it was tied to the relative game time, not wall time. Looking through the TorqueScript docs I printed I haven't spotted a getTime() function and speculative testing shows that if it exists its not called that.
#6
03/13/2005 (3:06 pm)
You can use getSimTime();
#7
03/13/2005 (5:27 pm)
Perfect. So set some variable = getSimTime(). (That way you can reproduce like Manuel said), and seed it in to get "true random". Beauty.