Bug Description
Hi there! While using math.random() it gives the same value, no matter if you rerun or restart the build. This is essential for my game as I’m aiming at releasing a game based around chance.
To Reproduce:
Just call math.random() and print it out, then replay and repeat. You should see the same values.
Expected behaviour (REQUIRED)
I’d expect lua to give me random values, not the same values.
Defold version (REQUIRED):
Version 1.9.8
Platforms (REQUIRED):
MacOS Apple Silicon
Minimal repro case project (OPTIONAL):
Tell me if you want it!
Logs (OPTIONAL):
INFO:DLIB: Log server started on port (X)
INFO:ENGINE: Target listening with name: mac.local - (X)
INFO:ENGINE: Engine service started on port (X)
INFO:GRAPHICS: Installed graphics device 'ADAPTER_FAMILY_OPENGL'
INFO:ENGINE: Defold Engine 1.9.8 (X)
INFO:DLIB: Initialized Remotery (X)
INFO:ENGINE: Loading data from: build/default
INFO:SOUND: Sound
INFO:SOUND: nSamplesPerSec: 48000
INFO:ENGINE: Initialised sound device 'default'
DEBUG:SCRIPT: Drawed 2
DEBUG:SCRIPT: Drawed 4
DEBUG:SCRIPT: Drawed 1
DEBUG:SCRIPT: Drawed 2
2025-03-24 13:20:46.010 Defold[X] +[IMKClient subclass]: chose IMKClient_Modern
2025-03-24 13:20:46.010 Defold[X] +[IMKInputSession subclass]: chose IMKInputSession_Modern
INFO:DLIB: SSDP: Started on address (X)
WARNING:DLIB: Failed to send announce message (-8)
WARNING:DLIB: Failed to send unannounce message (-8)
INFO:DLIB: SSDP: Done on address (X)
You could be right, I’m not well educated in this topic.
Other Engines, an instance Roblox Studio’s “luau”, simplify the math.random() library so developers interactions are simpler.
I can’t think of a context where a developer would want to use math.random() only once in the entire project. This is a shocking lua design choice I got to know only recently.
I hope Defold ignores the default Lua library and modifies it to fit a better developer experience. I picked Defold over GoDot because you guys provide the simplest programming language and are well optimized for 2D games. I think if you simplify the lua language for developers such as you, more people will use Defold.
as many times as you like. It will give you different values each time you run the game.
I hope I have been clear. Note that math.random is a key component in (almost) any game developed with Defold. It is very unlikely that it will not work as it should
Oh no, maybe I didn’t explain what I meant clearly.
I meant I and foreseeable plenty other developers would wish for just math.random() with no need for additional setup. If it worked out of the box, that would be so much better- of course this is just my personal opinion.
And, Luau also uses math.randomseed() to set the seed.
This is how random generators work. So no difference here.
And, the documentation refers to it (Also Luau’s documentation)
Hmmm, I thought luau automatically does that. When developers utilize math.random() in the Roblox Engine, developers have no need to input a random seed- it runs automatically and makes it more user friendly.
I’m not sure about the Defold’s design choice but if a step can be reduced- that would be awesome.
The problem is when I refresh the build, I get the same numbers instead of random new ones- at least this is the behavior in MacOS Apple Silicon right now.
Code:
function init(self)
print("1:",math.random(1,11))
print("2:",math.random(1,11))
print("3:",math.random(1,11))
print("4:",math.random(1,11))
print("5:",math.random(1,11))
end
To you it’s a feature but to me it’s something that could be simplified. I have no ill intent suggesting this solution and I do not want you to misinterpret my engagement for yapping aimlessly. I don’t feel heard and I’ll stop communicating on this matter.
Let me try to bridge the gap here because I don’t think we’re understanding eachother.
There are two legitimate ways to use the random function:
Not providing a seed, meaning the sequence of random numbers is repeatable (default behaviour)
Providing a seed based on os.time, meaning the sequence is different every time.
@SuperIdeaDeveloper (rightly) thinks that variant 2 is more common in practice. Therefore, SuperIdeaDeveloper believes 2 should be the default behaviour in Defold, not 1.
On the other hand, the Defold devs haven’t made a habit of modifying default Lua behaviour. There is an expectation that Defold users acquaint themselves with Lua when using Defold. On this basis, the behaviour will not be modified.
I understand you and I think you’re right, setting the seed for the developer would make using math.random() easier. But I also think it would make other things harder.
For example, if you were debugging errors. You might need to know when the seed is set and how. Does each module have its own seed? What method are they using to set the seed? Can a player find their seed and send it to you? If your game fails on seeds that are divisible by 51729 can you solve the issue?
But Defold’s design is to make the developer answer those questions. Maybe more aggressively than other engines, but in my opinion it pays off by getting in your way a lot less often.
Edit to avoid a bump: Before posting originally, I removed explanation of how math.random() works. The highlight is that I think it’s important to keep in mind that computers can only generate pseudo-random numbers, and I personally think that setting the seed helps me remember that.