Problem with math.random

I am trying to generate a random number between 1 and GameID

      local GameID = GetData()
      TICK = tick()      
      math.randomseed(TICK)
      local RANDOM = math.random(GameID)
      local GameData = f(RANDOM)

but I keep getting this error
I only get this error in game, but not in studio.
and from the gyazo image GameID is not nil
I am being told it is an overflow error

Im not sure what GetData is, couldn’t you just workspace.GameId (this is probarly wrong) or a property of that sort
Its probably erroring because GetData() returns something else like a table or something instead of a number value

Have you tried printing GameID before that line?

math.randomseed(TICK)
print(GameID)
local RANDOM = math.random(GameID)

If it prints something like inf or NaN, it probably is an overflow error like you said.

GetData is a function that returns the UniverseID of the last created roblox game and according to output it is clearly a number

1 Like

local RANDOM = math.random(1, GameID)

GameID can’t be less then 1

That error means you’re passing a number smaller than 1.
Make sure that GameID isn’t by a chance 0 for any reason.

It would help if you showed us the source of your GetData function

Supposing that GameID returns an array, I would do

local randomid = math.random(1, #GameID) 
local gameidgenerated = GameID[randomid]

That isn’t the case. Otherwise the error message would be different.

Yeah he is printing it and it is not a 0, weird

I tried printing math.random() with the specific GameID value that was there, if you remove a digit, it seems to work fine, perhaps you’re trying to access values that are too high? Though, that doesn’t explain why that error is read like that. Will experiment to find out whats the highest possible number to use.

EDIT: Subtracting 1 from the value seems to work.

math.random has problems with overflowing with numbers greater than (2^31)-1. You should use Randoms instead for large numbers since it doesn’t have that issue.

That’s a bug with math.random but it’ll have to be dealt with separately from this thread.

5 Likes

If GameID isn’t a constant, you can seed it to Random.new

local GameID = GetData()
local RANDOM = Random.new(GameID)
GameData = f(RANDOM:NextNumber()) --not an int

Alternatively,

local GameID = GetData()
local GameData = f(Random.new():NextInteger(tick(), GameID))

Edit: You will get the same sequence of numbers if GameID (or whatever the seed is) is static.

I tried math.random with that number in an online lua compiler and it worked.
Lua 5.3 while roblox uses 5.1.4 i think so yeah they must’ve fixed this.