Random.new() or math.random()

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

10 Likes

Personally i use math.random().

1 Like

I think that math.Random() is the best one. (I Recommend This)

1 Like

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

1 Like

In my opinion math.Random() is the best, to the point that I didn’t even know that Random.new() existed, because I never needed it in the first place.

2 Likes

Random.new() is actually newer, so a lot of people dont really know about it

3 Likes

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.

from here: A Random Feature

and it even has separate functions for integers and doubles (NextNumber and NextInteger)!

2 Likes

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).

24 Likes

I’d prefer to use Random.new

Randow.new() is a tiny bit more complicated than math.random, but It can chose a decimal between two numbers as shown like this

local number = Random.new(tick()):NextNumber(1,5) 
print(number)
-- will return 1, 5, or anything inbetween

With math.random, I’d use this stupid code.

local number = 1 + (math.random(1,8) * .1)
print(number)
6 Likes

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.

2 Likes

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

Do this with Random.new()

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

1 Like

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

5 Likes
Random.new()

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:

RandomResult:NextInteger(x,y)
RandomResult:NextNumber()
RandomResult:NextNumber(x,y)
RandomResult:NextUnitVector()
RandomResult:Clone()

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.

1 Like

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

1 Like