evanbowman / BPCore-Engine

Lua game framework for Gameboy Advance
The Unlicense
164 stars 10 forks source link

math.random() is not random #17

Open nguyengiabach1201 opened 2 months ago

nguyengiabach1201 commented 2 months ago

math.random() in Lua will return pre-generated pseudo-random numbers. But in normal case, we can use math.randomseed(os.time()) as a walk-around. But we don't have os.time() function for GBA. Please help!

evanbowman commented 2 months ago

Hi! Unfortunately, the GBA does not have an operating system, which makes it difficult to provide much of the os library. Even professional quality GBA games did not have a reliable source of seed values for random number generation. Many GBA games would use tricks, like invoking the random number engine each frame while waiting for user input on the title screen, to introduce some additional unpredictability into the state of the random number sequence. I can try to give you some more suggestions for workarounds for seeding the RNG. But: 1) The GBA does not have a system clock, unless there is a hardware clock built into the GBA cartridge that is running a game, so there is no reliable clock source value to return from os.time(). The makes it impossible to provide a good implementation of os.time() 2) The GBA hardware always generally starts up in the same state. Therefore, there is no source of hardware randomness on the GBA that I can use to provide a seed value.

Please feel welcome to ask additional questions, or to propose new changes in the future. For this issue, other than suggesting possible workarounds, there is, regrettably, not much that I can do to provide a random seed.