Interesting little tidbit- math.random

Interesting little tidbit- math.random

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Interesting little tidbit- math.random

Post by Argh »

Just a little bit of random programmer lore I thought I'd share with folks, so that maybe they can avoid the newbie trap I hit yesterday:

Math.Random using a low range of floats can cause an error in synced code. I.E., math.random(-0.3,0.3) will result in an error, where Lua reports an "empty interval" and ends the program.

This is due to Spring's handling of math.random, which is as follows:

Code: Select all

        else if ((args == 2) && lua_isnumber(L, 1) && lua_isnumber(L, 2)) {
                const int lower = (int)lua_tonumber(L, 1);
                const int upper = (int)lua_tonumber(L, 2);
                if (lower >= upper) {
                        luaL_error(L, "Empty interval in math.random() {synced}");
                }
                const float diff  = (upper - lower);
                const float r = gs->randFloat(); // [0,1], not [0,1) ?
                int value = lower + (int)(r * (diff + 1));
                value = max(lower, min(upper, value));
                lua_pushnumber(L, value);
        }
Anyhow, it appears that math.random can only return integers, so if you need a fairly wide range of numbers between -1.0 and 1.0, you can avoid it with simple math like this:

Code: Select all

local myVariable = math.random(-100,100)
myVariable = myVariable / 100
Which, given Lua's un-typed handling of variables, will by default return a float value between 1.000000 and -1.000000.

So, if you've been doing anything on the assumption that math.random's results are a float, such as using max, min or floor... there isn't a need, apparently, unless you're performing operations on it afterwards and need it to become an integer or something.

Just thought I'd throw that out there for folks, feel free to correct my terminology, etc., if I didn't write a sufficiently-clear explanation of what's going on programmatically.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Interesting little tidbit- math.random

Post by zwzsg »

Wow. Argh strikes again.

From http://www.lua.org/manual/5.1/manual.ht ... ath.random
math.random ([m [, n]])

This function is an interface to the simple pseudo-random generator function rand provided by ANSI C. (No guarantees can be given for its statistical properties.)

When called without arguments, returns a uniform pseudo-random real number in the range [0,1). When called with an integer number m, math.random returns a uniform pseudo-random integer in the range [1, m]. When called with two integer numbers m and n, math.random returns a uniform pseudo-random integer in the range [m, n].
I guess we are supposed to kiss your feet for your holy offering of insight, Argh. What would we do without you? We were all trying to get random integers between 0 and 0 and could not understand why it crashed, but you saved us! You're such a genius! I think you should really try making games for a living, you're so good at it.
User avatar
REVENGE
Posts: 2382
Joined: 24 Aug 2006, 06:13

Re: Interesting little tidbit- math.random

Post by REVENGE »

i am laughing in real life
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Interesting little tidbit- math.random

Post by Argh »

<shrugs>

Was just trying to be helpful, this wasn't a problem where the cause was immediately obvious. The error "empty interval" isn't something I'd seen before.

It doesn't say something helpful like, "hey, I only return ints, stupid", it says something that only makes sense if you know what an interval is.
Post Reply

Return to “Lua Scripts”