PDA

View Full Version : Get a random number?



Blacky
March 18th, 2013, 11:07
Is there some limitation, or built-in tool to generating a random number?

For example I tried that, based on some basic LUA doc:


math.randomseed( os.time() )
math.random();


Which returns: Runtime Notice: Script Error: [string "scripts/chatmanager.lua"]:345: attempt to index global 'os' (a nil value)

So I'm guessing in FG2, I can't access “os”.

Any way to generate reasonably good randomness ?

S Ferguson
March 18th, 2013, 17:20
That doesn't work because there is no way in which to access the system clock from inside FG (The os library isn't available). The random number is still there, however, as the math libraries are included (and the die rolls are random). It depends on what you need it for.

Blacky
March 18th, 2013, 17:31
It could be used for a lot of things, here it's for a combat location hit script.

Is there some tools inside FG to provide math.random() with some randomness (I'm guessing something was written to have fairly random dice rolls, at the very least, beside the basic random() of the various languages which mostly aren't random at all)? I've read some Lua test, even with os.time as a seed, it really isn't random. Without it, I'm afraid what the result will be… well, not good. Or a Lua trick to generate a somewhat random seed…

I would rather avoid hitting the head 40 times in a row :)

S Ferguson
March 18th, 2013, 19:02
The math.random function delivers a pseudo-random (the best your going to get) in the range of (0,1). the randomseed is optional and only provides minimal change.

If you call random with a numerical argument N the pseudo-random number will be in the range of 1 <= X <= N. There are a few mathematical methods of generating a pseudo-random seed, but for all intents and purposes it's not worth it.

As long as there is no randomseed value you shouldn't get any repetition over the course of games. I suppose you could use the random function to generate a pseudo-random number for each game like pulling 6 numbers from a hat....