A Better Random Number Generator

As a Roblox developer, it is currently too hard to create truly random numbers.
The current pseudo random number generator math.random() has many issues. Even when using a randomseed, the generated numbers are not always pseudo random, and there are some bugs in which duplicate results are returned.

If Roblox is able to address your issue, how would it improve your game and/or your development experience?
In a modern game, random number generation is used quite frequently for things such as choosing random maps, rewards, players, damage, and many other things. As a developer, I need a random number generator that I know is really random. We owe it to the players to provide random results that are as random as possible.

Surely with all your expertise, and with so many proven implementations of random number generators, Roblox should be able to upgrade the math.random() function to something better.

25 Likes

We’re working on this.

46 Likes

@Fractality_alt

2 Likes

OwO

Looking forward to this

3 Likes

Here’s the gist:

  1. We’re exposing a Random class with discrete state and a high-quality algorithm.
  2. We’re giving math.random a better (platform-independent) algorithm and per-script state.

Here’s a Fisher-Yates shuffle using the new API

local rng = Random.new()

local function Shuffle(t)
	for i = #t, 2, -1 do
		local j = rng:NextInteger(1, i)
		t[j], t[i] = t[i], t[j]
	end
end

I’ll make an announcement post with more details when the time comes (eta <= 1 mo). Feel free to voice any questions/comments/concerns.

34 Likes

What kind of methods/properties will the new Random API have?

Will Random class API be accessible with intellisense?

Yeah, it works with intellisense.

For sure! This needs to be added. My game, even with math.randomseed(tick()), the spin system will keep spinning the same magic abilities in the same order when you rejoin.

Looks a lot like how C# does it. Whenever you wanted to use better RNG in it you’d use the cryptography class.

1 Like

Try math.randomseed(os.time() % math.random(1, 10))

3 Likes

Make sure it’s __namecall invoked because fast math is best math!
Also @MapleMarvel I’m fairly sure Lua’s math.random implementation is just Rand() (from C math) wrapped as a Lua function.

1 Like

Yeah, unfortunately math.randomseed currently turns subsequent calls to math.random into deterministic junk.
That was one of the motivations for the work being done now.

1 Like

Is math.randomseed doing something that can cause the seed to duplicate accidentally?

I would expect all random algorithms to return the same number series given the same seed, which can be very useful.

Many multiplayer games rely on being able to seed random with a known number and get the same series out of it across clients to keep things in sync. I know Doom and Quake had their own custom random generators for just this reason, to ensure the random stream would be the same for all clients regardless of their platform (they couldn’t rely on the implementation of clib random being the same on Mac, Windows, Linux etc. etc. so they rolled their own).

It also means dynamically generated worlds will create the same world given the same seed every time. You don’t need to store the world, just the seed.

I’ve also worked on more than one project where the random seed was part of bug reports to help reproduce issues.

1 Like