"interval is empty", Lua math.random isn't working for large numbers?

Houshalter

I didn't know if this is a bug in Lua itself or if I was doing something wrong. I couldn't find anything about it anywhere. I am using Lua for Windows (Lua 5.1.4):

>return math.random(0, 1000000000)
1251258

This returns a random integer between 0 and 10000000000, as expected. This seems to work for all other values. But if I add a single 0:

>return math.random(0, 10000000000)
stdin:1: bad argument #2 to 'random' (interval is empty)

Any number higher than that does the same thing.

I tried to figure out exactly how high a number has to be to cause this and found something even weirder:

>return math.random(0, 2147483647)
-75617745

If the value is 2147483647 then it gives me negative numbers. Any higher than that and it throws an error. Any lower than that and it works fine.

That's 0b1111111111111111111111111111111 in binary, 31 binary digits exactly. I am not sure what that means though.

greatwolf

This unexpected behavior (bug?) is due to how math.random treats the input arguments passed in Lua 5.1. From lmathlib.c:

case 2: {  /* lower and upper limits */
  int l = luaL_checkint(L, 1);
  int u = luaL_checkint(L, 2);
  luaL_argcheck(L, l<=u, 2, "interval is empty");
  lua_pushnumber(L, floor(r*(u-l+1))+l);  /* int between `l' and `u' */
  break;
}

As you may know in C, a standard int can represent values -2,147,483,648 to 2,147,483,647. Adding +1 to 2,147,483,647, like in your use-case, will overflow and wrap around the value giving -2,147,483,648. The end result is negative since you're multiplying a positive with a negative number.

Furthermore, anything above 2,147,483,647 will fail the luaL_argcheck due to overflow wraparound.

There are a few ways to address this problem:

  • Upgrade to Lua 5.2. That one has since fixed this issue by treating the input arguments as lua_Number instead.
  • Switch to LuaJIT which does not have this integer overflow issue.
  • Patch the Lua 5.1 source yourself with the fix and recompile.
  • Modify your random range so it does not overflow.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Lua - math.random () isn't working

From Dev

Lua math.random not working

From Dev

Why isn't Math.floor((Math.random() * 999999) + 100000) reliable for producing 6 digit numbers?

From Dev

Trying to make a math game, but the math isn't working

From Dev

import random isn't working (I think)

From Dev

import random isn't working (I think)

From Dev

Javascript comparing numbers isn't working

From Dev

Large numbers - Math in JavaScript

From Dev

Some simple math isn't working in my helper

From Dev

Function template isn't producing random numbers properly

From Dev

My function for generating random numbers isn't going past 500000

From Dev

Function template isn't producing random numbers properly

From Dev

Lua Math.random and if statement

From Dev

Math.random not working

From Dev

Python: why the code for swapping the largest and the smallest numbers isn't working?

From Dev

Method for checking if string is getting empty text or numbers isn't working

From Dev

Lua C API: pushing function with lua_pushcfunction isn't working

From Dev

Generating uniform random numbers in Lua

From Dev

Generating uniform random numbers in Lua

From Dev

PHP math with random numbers with random mathematical symbols

From Dev

Working with very large numbers

From Dev

Random math generation algorithm not working

From Dev

Lua math.random returns erratic values

From Dev

Redis Lua Script math.random

From Dev

math.random function in lua to position object

From Dev

math.random function in lua to position object

From Dev

fscanf returning random large numbers

From Dev

GetProcessesByName isn't working

From Dev

INSERT isn't working

Related Related

HotTag

Archive