Ok so i want to know which of these is better?
Random.new() or math.random()
And if one is better than the other, then how so?
If one is not better than the other,
i dont really see the point of having added Random.new() when
they could have just added onto math.random()
And i would like to know why they wouldnt have done that instead
– if i posted in wrong section, admin please let me know
I’m using math.Random() too. It’s the best I can think of, maybe I’m just using Random.new() wrong, but when I want to print the result of Random.new() it’s just printing “Random” xd
hmm, i just learned Random.new() from this post. i think i will use it instead, because it seems to be newer and more reliable due to technical details:
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.
math.random() has been update to use the same random algorithm as Random.new()
The decision mainly comes down to “fairness” as math.random() is a global random object. Essentially the state of the random object changes as you get the next pseudo random in the sequence so using the global random though convenient is not goint be be fair because other scripts may or may not call the next pseudo random in the sequence.
Random.new() create a new random object so you can guarantee the “fairness” as no other script has called the next pseudo random (unless you allow it).
I prefer using Random.new() since it’s newer and more reliable. If there was some kind of bug they had to fix in the algorithm, they’d probably take more time to do the same thing to math.random(), and they’d update Random.new() first.
Hope I helped! If I made any mistakes, please tell me.
math.random has been updated to use the same algorithm, so they’d receive updates at the same time going forward. The only reason they didn’t update math.random instantly is to ensure there were no problems with the new algorithm in case it broke every game with math.random. It worked well so they are now the same in terms of maintenance, and the only difference is the scope of the pseudorandom sequence.
The actual benefits have been discussed by other replies, and generally amount to better performance and the ability to control multiple pseudorandom sequences independently of one another.
math.random() is best for making randomised number values.
Example:
local randomNumber = math.random(1,80)
print(randomNumber) -- Prints random value between 1 to 80
if randomNumber >= 40 then
print("Higher than/Equal to 40")
else
print("Lower than 40")
end
local random = Random.new(tick())
local randomNumber = random:NextInteger(1,80)
if randomNumber >= 40 then
print("Higher than/Equal to 40")
else
print("Lower than 40")
end
I personally used Random.new() since i tried math.random() in my randomly generated obstacle course and the course seems to be predefined by the game which means that everytime i joined the game its the same stage as i encountered last time
did you put math.randomseed(os.time()) somewhere? you need to set the seed for the random generator, otherwise the seed will be the same and you will get the same results
This function has the potential to create decimal precise random numbers with multiple methods, while math.random only generates a whole number between a range of x and y.
At first, it may seem useless, as any attempt to use the function will return something like: Random
Ill use the name RandomResult to seperate it from the actual ‘Random’ table.
Though that is because a method must be applied before it is an actual number.
There are 4 ways to get the random number as of now, and those would be:
Though the names should hint as to what they can be used for, I highly recommend reading this page for more information on how they can be applied.
Edit: I forgot to mention, but math.random does also have an issue where using large numbers for the minimum and max values can cause the function to error, saying that the interval is empty.
Specifically, the maximum number is 2^31-1 or 2147483647 and the minimum number is the negative version -2147483648. However, setting the min and max parameters to its minimum and maximum values causes problems.
Only when a parameter is given. math.random can also work similar to RandomObject:NextNumber