I've been using math.random() in my game and I find that it certain results are much more like than others. For instance, in math.random(1,100) I may see 81 and some other numbers but never 1 and the rest.
It's not on the Wiki yet, but there was an announcement on the DevForum about a new random number generator: Random
. The summary is that:
randomseed
affects every script in the DataModel that uses math.random
.Here is how to use it:
-- Print 50 random numbers between 1 and 100 local generator = Random.new() for i=1, 50 do print(generator:NextInteger(1, 100)) end
The announcement also mentioned that Roblox plans on integrating the new algorithm with math.random
by this year, but using Random.new()
as in my snippet above is probably preferred at this point.
The other answers here aren't totally correct, unless you want to use the new random feature Roblox has added.
math.randomseed(os.time());
Now, you can call math.random(10, 15)
for example, and get a random integer between 10 and 15.
Nothing can be truly random on a computer, but for more "random" / accurate results, always use randomseed once. It's important to note that tick() should not be used in place of os.time().
If you are doing math.Random
, you set two numbers that you want. The numbers between the two you give, will be the numbers the computer gives you in the output. Unless it is closer in range. A computer or a program that operates in lua, now I don't wanna get too deep here, a computer or program that operates lua is given a math function. That math function is used to find numbers in range to the computer. For you to do math.random(1,100)
, since the first number is one, or two, it would take half of the second number, being 50, and make a random number near 50, but since you already set the perimeters of 1 and 100, it doesn't like to get too close. But if you have it closer like (1,5)
, it would sorta have no choice. That's why you're not getting things like one or 2. Hope this helped :)