The Random object and math.random share the same algorithm. The Random object is good for holding state and branching results however, which the math.random object doesn’t do. Seeding is global to all uses of math.random, whereas Random objects can have their own seed.
Passing a seed in the constructor is only necessary if you need deterministic randomisation where the results of each iteration are the same, otherwise leave it blank. The seed will automatically be randomised from a source of entropy for every new Random object, it is not necessary for you to do that.
For example: if you needed Random results but you wanted the pattern the same, you could pass a seed of 500
. Every time you make a new Random object and pass 500, the generation pattern will be the same. If it generates 5, 9, 1, 3
and say 20 minutes later you make another Random object with the 500 seed, it will also return 5, 9, 1, 3
.
Random gives you the ability to effectively make randomisation local to that object, whereas math.random is global. If seeding is irrelevant and you aren’t making much or any use of math.randomseed, you can continue to use math.random. If you’re looking for more options, use the Random object.
In your case of generation, you can just directly use the Random object and avoid wrapping it in a function, really not that necessary to include your own function in your utilities module. That being said, what you’re doing now is no different than simply just using math.random. If you want to retain the seed you’ll need to return a function. At the very least, you won’t be creating a new Random object every time, but you won’t be globalising the seed either.
--- Optionally add a parameter for seeding if you require it
function utilities.getRNG()
local RandomGenerator = Random.new()
return function(min, max)
return RandomGenerator:NextInteger(min, max)
end
end
local generateRandomNumber = utilities.getRNG()
local newNumber = generateRandomNumber(1, 10)