What is the Luau math.random() function?

  1. What do you want to achieve?
    Roblox isn’t very good at image generation and I wanted to make a map generator to visualize my game’s procedurally generated map so I coded something in normal Lua, but doing randomseed(0) and printing math.random() on both seems to give different results.
    Is there any way to get the same math.random() results on default Lua?

  2. What is the issue?
    I can’t find anything about it anywhere.

  3. What solutions have you tried so far?
    I tried looking around but I haven’t found anything about it besides a million “is math.random or random.new better?” posts.

EDIT: I specifically need the function used in Luau, not the one from Lua. The default Luau math.random() is probably made to be faster for Roblox, and the map generator doesn’t need to be fast, because it’s only running a single time.

2 Likes

Random.new takes a seed parameter. Create a base Random.new object with any seed as the parameter, and everytime you call :NextInteger or :NextNumber it will provide identical results (as long as you’re using the same range). Without the parameter, it will generate a seed everytime one of its methods are called.

1 Like

I said I’m running it on default Lua, you can’t make a PNG map generator in Roblox. The issue is that the functions are different, not that I’m coding it incorrectly.

In that case, math.randomseed is not necessary. Notice how everytime you call math.random (in vanilla Lua) without calling math.randomseed will return the same result every time (within the same range).

Otherwise if i’m misunderstanding, math.random is sequential from its the randomseed, every time you call randomseed(n). For example, if you call randomseed with a number argument and print math.random 3 times, you will have a sequence of 3 random numbers. If you call randomseed again afterward with the same number argument, and print math.random 3 times like previously, you will receive the exact sequence as the first three math.randoms

This behavior is why you’re receiving different results. If you do your test again, but this time calling math.randomseed(0) before each time you call math.random, you will receive the same result from both math.random calls.

I already said in the post that I called math.randomseed(0) before the math.random() call. Are you sure you read it completely?

I said what I need is the function. Luau and Lua use different math.random() functions. I do not need anything else.

I mentioned to call it both times

I was talking about vanilla Lua

math.random() is only called once per script. The issue is that it’s different in 2 different scripts, one on default Lua, and the other on Luau. I know how it works, you don’t need to tell me.

That doesn’t matter, I’m saying the reason why it’s different is because of that, not that you’re talking about the wrong one.

Please read the post before answering.

Potentially, if you want the math.random which is deployed in LuaU, you could go and read the Source of LuaU :smile:

Potentially this helps; This is what i’ve found of the math_random call the LuaU VM makes

1 Like

2 Likes