How To Predict Math.random()/What Algorithm It Uses?

I need to predict randomness. I want to make a script that will work in the same way as math.random() and will give the same values. So far I’ve been trying to find out what algorithm is used for randomization in lua using Chat-GPT and it gave a formula that can be used to find the next seed “X(n+1) = (a*X(n) + c) mod m” but it can’t name the coefficients a, c and m besides I’m not sure if lua really uses this algorithm also, as far as I understand, either the entire algorithm or the coefficients have been replaced in the studio because the random results with the same seed in pure Lua and in the studio differ.
So now I’m trying to find an algorithm that is used for randomness or some other way to predict randomness. I`ll be glad if someone know something about this.

EDITED: My biggest goal is to find a way to get seed from values that it produce so I need the algorithm that makes value from seed to reverse it

2 Likes

“a” and “c” are simply constants used in the equation. I’m pretty sure they can be any number. M is another constant but is usually a large prime number

1 Like

Thanks for your response! Yeah but ChatGPT gives me like random numbers (like 12345). I was thinking about making some sort of equation to find them, but haven’t done it yet

1 Like

Roblox, to my understanding, uses Permuted congruential generator (PCG for short) and uses the variant XSH-RR

3 Likes

Use a Random object with a known seed.

local rng = Random.new(123)
print(rng:NextNumber()) -- 0.5576342002772471
print(rng:NextNumber()) -- 0.56777438777716
print(rng:NextNumber()) -- 0.932357652636514
rng = Random.new(123)
print(rng:NextNumber()) -- 0.5576342002772471
print(rng:NextNumber()) -- 0.56777438777716
print(rng:NextNumber()) -- 0.932357652636514

You should’ve asked ChatGPT to tell you what the algorithm was called so that you can research it.
I think it gave you the formula for a Linear Congruential Generator.

2 Likes

You can see the exact algorithm used here:

It is probably hard to re-implement in lua because as far as I know lua doesn’t support 64 bit ints, shifts, bitwise or, or bitwise xor.

edit: Updated link, was pointed to the wrong rand function

2 Likes

Thanks! Thats seems very interesting. I didn’t delve deep enough into Java to understand everything that happens here, but as far as I understand, random numbers from Java are used here and we enter a loop. Correct me if I’m wrong

1 Like

You can predict random by using the set seed function. The set seed function lets you get predictable random numbers.

You can use math.random and set seed to predict math.random, as long as you have the right seed. I believe Roblox uses the tick() as the seed, though I’m not sure on the specifics.

Are you trying to use randomness outside of Roblox, so you need an identical function to match inside Roblox? I would implement a custom randomness function in that case. It’s not possible to predict the random function without its seed (or a set of random numbers and some compute).

2 Likes

The Random object is by design meant to be as unpredictable as possible. It’s an implementation of a PRNG (psuedorandom number generator) which means that it is technically deterministic. Others in this post have explored this fact in more detail.

Again, as others have pointed, the only way you are going to be able to meaningfully predict a Random object’s output is by knowing its seed.

Even then, you can’t predict what the next state of the object will be without knowledge of its previous states. If you need to predict randomness, just use the Random object and stick to a seed you will know at runtime.

I wouldn’t reinvent the wheel here - just use the Random object with a seed. If you really need to, you can read the implementation on the github repo (someone else linked it), but as they mentioned ROBLOX’s Lua variant lacks some of the required operations/primitives to properly compute the random values.

3 Likes

Thanks for response. Actually i don’t need to do it in lua, preferably in python. Also maybe I should have explained my goal more specifically.

1 Like