Yes, there are other topics about this and no, they haven’t helped me.
First of all, what does Random.new have that math.random doesn’t? I’ve read multiple times that it’s a better way of getting a random number but i dont understand why.
Second of all, what’s the point of seeds?
local seed = math.random(1000) -- I've seen people use scripts like this as examples
local RandomNumber = Random.new(seed):NextInteger(1, 2)
print(RandomNumber)
This will always print the same random number between 1 and 2, because i give it the same seed. What’s the point of the seed then? The whole point is getting a random number, why does the seed make it the same number everytime?
Also, if we are gonna use math.random() to get the seed, then why use math.random to get the random number instead of doing all this?
RandomNumber = math.random(1,2) -- What's the difference?
Random.new() returns a constructed random object with the passed seed, or using voltage entropy on the hardware if a seed isn’t provided. This allows you to have multiple seeded random objects simultaneously, whereas math.randomseed() will overwrite the seed for every single call to math.random.
This is the point of seeds, it’s called deterministic random. The seed allows you to repeat the same random results if you call the random methods in the same order with the same parameters. One commonly used pattern for this is replicating the same results at another point in time. A good example is Minecraft world seeds. Because Minecraft’s terrain is generated with deterministic random, you can share a seed with a friend and get the same random terrain generation.
Alright thanks for the answer. Currently i have no use for that, i just need a random number that isn’t going to repeat itself. Do i need to use Random.new even in that case?
Also, i wasn’t really using math.randomseed, is that a problem? Sorry for repeating myself, but i have read multiple times people say stuff like “use math.randomseed if you want a true random number”, but i don’t want the whole number repeating itself thing we just discussed.
Doing math.random() without any arguments will return a decimal value between 0 - 1 (ex: 0.4465451985154175), you can use that to significantly reduce the chances of a number repeating since there’s functionally an infinite amount of possibilities
I would also like to note that Random.new() is optimal for randomness in networking. Since Random.new(seed):NextInteger(1, 2) will be the same no matter which client runs it, you can use it to have seemingly “random” results that will be the same between clients/servers.
For example, if you needed to make a random bullet spread and needed it to replicate the same to other clients, you could send over a random seed and get the same spread pattern for each time it’s used.
Originally the Random object was more random than math.random, but Roblox changed math.random to use the Random object’s implementation so now it’s just a matter of personal preference. Random.new() will be seeded using a hardware entropy source (almost always a voltage sensor), which makes it effectively the “most random” you can get with the included API.
Not at all, no. You don’t have to seed math.random, or pass a seed to Random.new().