A Random Feature

Hey everyone,

In observance of the gift-giving season, we’re releasing a new Lua datatype: Random.
Random is used to generate high-quality pseudorandom numbers from a dedicated internal state.

API


Random.new([double seed])

Creates a new Random object from the given seed.
Multiple Randoms seeded with the same value will always produce the same number sequence.
If a seed isn’t specified, one will automatically be pulled from an internal entropy source.

int Random:NextInteger(int min, int max)

Returns a pseudorandom integer uniformly distributed over [min, max].

double Random:NextNumber([double min=0, double max=1])

Returns a pseudorandom number uniformly distributed over [min, max).

Random Random:Clone()

Returns a new Random object with the same state as the original.
This method can be useful for branching off the results of the RNG.

Technical details


The algorithm used by Random is the XSH-RR variant of the PCG family.
In a nutshell, it’s is a fast algorithm with excellent statistical properties.
We’re planning to change math.random to use the same algorithm by early next year.

Examples


Flip a coin

local rng = Random.new()
local function CoinFlip()
	return rng:NextNumber() < 1/2 and 'Heads' or 'Tails'
end

Shuffle an array

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

Generate a random color

local rng = Random.new()
local function RandomColor()
	local r = rng:NextNumber(0, 255)
	local g = rng:NextNumber(0, 255)
	local b = rng:NextNumber(0, 255)
	return Color3.fromRGB(r, g, b)
end

Feel free to post questions and comments; our team would love to hear your feedback.

This is live. Happy holidays!

220 Likes
Impossible to find documentation for the Random class
Math.randomseed randomness
Making NPC move randomly using AI Pathfinding service?
How come this only spawns one item?
Adding value not working
How can i make a TextLabel display a random number?
A Simple Guide to Randomization. (Position, Tables, Values)
A Simple Guide to Randomization. (Position, Tables, Values)
Help on a random chance feature [Egg hatching system]
Is math.random enough for a % system?
Math.random() or Random:NextNumber/Integer()?
How can I prevent the NPCs from having the same outfits?
Query regarding tabling and teleporting
More precise % chances
Math.randomseed seemingly broken?
Table only chooses ONE color
TrueRandom - A Solution to Pseudo-random Numbers
Generating Values Within Range from Seed
Can players just be extremely lucky with math.random() if they continuously do good by chance?
How do you make math.random() print decimals?
What is math.randomseed()
Is there a better way of making a fair 50/50 chance script without using math.random()?
What's the point of Random.new():NextNumber()? Random vs. math.random()?
How would I implement this aloroghithm in LUA?
Make your own custom wait function! (FASTER THAN 1/180 of a second!)
Math.random() how to have decibels?
Should math.random not be used for new work?
Random.new() or math.random()
Humanoid is not a valid member of model?
Random.new() or math.random()
Weighted crate system not choosing an item of the same rarity as another
Best way to select maps
NEW PROBLEM - Gui wont change
Handcuffs system - Help me make it more efficient!

YES i am excited

12 Likes

Pretty cool :sunglasses:

5 Likes

Awesome!! :+1:

5 Likes

That’s sweet!

2 Likes

Wow that local Name do stuff is blowing my mind I gotta test this real quick

And awesome can’t wait to use

3 Likes

Well, that was random. Cool!

17 Likes

can’t wait to be accurately random!
:thinking:

9 Likes

Can the state be replicated between sessions, or saved with a place/model, or be read and set?

9 Likes

You guys had better make me proud and make cool stuff with this!

9 Likes

Random Simulator

Front page, let’s make it happen

9 Likes

State doesn’t replicate/serialize/persist for now. That’s something we could totally look into if there turns out to be a strong-enough case for it.

For now, you can get similar results by saving the initial seed along with a count of how many times NextInteger+NextNumber have been called.

3 Likes

Are you making any sense of the local Name do thing? Because I’m not. How the hell does it work lol.

1 Like

local x = 0

do
local y = 3
x = 2 + y
end

print(x, y) -- 5, nil

He’s doing it like that so rng only exists in that do scope.

2 Likes

What’s it good for? Memory optimisation?

1 Like

Well, here’s a breakdown of how it works:

local Name – Stores the variable in the script

Now, a do statement is basically useless except for one thing: Scope.
The scope of a variable is where it can used - it has to be used within its scope.

When the scope is done, all variables assigned to it will be removed from the memory (I assume). The Name variable is out of the do's scope so it doesn’t get removed.

2 Likes

That name oddly scared me for a moment.

EDIT: OH OH CLEVER TITLE

3 Likes

It still exists in memory, since the function is using it. But it helps not pollute the global scope. So if you wanted to define multiple functions that use their own instances of Random this is one way to do it.

5 Likes

I see, that’s good to know. Thanks! ( + @EmeraldSlash )

2 Likes

This is a very awesome update. I can’t wait to test it out. :slight_smile:

1 Like