Random seed

I am using math.randomseed to ensure to have the same random numbers across devices.

	math.randomseed (1)
	native.showAlert ("Solar2D", math.random (1, 10))

On macOs it prompts 2, but on a samsung tablet it prompts 4.
Am I missing something?

The system-defined random number generator doesn’t have any canoncial implementation or properties. You can see this in math.random()'s docs: " This function is an interface to the simple pseudo-random generator function rand provided by ANSI C. No guarantees can be given for its statistical properties." So you’re probably getting two different techniques.

If you do need consistent numbers, I have an old plugin for that. You can use MakeGenerator_Lib() to return a function that can be called like math.random(). (See the examples linked from that page for seeding, etc.)

(Plugin code)

You can also take a look at this:

Somewhat related: this issue of the wfc library. Another fellow and I both mentioned to the author (in a separate issue and PR, respectively) that it stomps the system random seed, so he’s been doing some investigations into alternatives. I recommend the GDC talk if you’re interested in this.

(I haven’t put any docs together yet, but have a plugin wrapping wfc, with a test here.)

Probably a little late here but we found the same: it’s not consistent across platforms.

Try this: GitHub - linux-man/randomlua: Pure Lua Random Numbers Generator

1 Like

+1 for the randomlua generator. We’ve been using it for about 3 years, and it always gives us consistent random numbers for a given seed across all platforms.

My module that bgmadclown links to uses the same linear congruential method as the more recently linked randomlua, without any of the extras. Here’s a direct link to my module:

The page that bgmadclown linked visualises the randomisation results (and it has links to the aforementioned Lua module, as well as a JS module).

My advice to anyone who reads this post is to keep on using Lua’s built in math.random, as it is written in C and its faster, as long as they don’t need to have consistent pseudorandom numbers across different platforms. If they do, then the linear congruential method is more than enough, but just keep in mind that it’s not cryptographically secure or “truly random” by any means. If you are using it for things like seeding level generation, etc. then it’ll work perfectly.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.