Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

math.random() isn't truly random?

Asked by 6 years ago
Edited 6 years ago

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.

0
Try calling math.randomseed(tick); in the code above ur math.random() statement. KingLoneCat 2642 — 6y
0
I tried that too (sorry), but that didn't work either. I put it at the very beginning of the script. theDEVILjsb -10 — 6y
0
Oh, I wrote math.randomseed(tick()), not (tick). I've never seen (tick), should I still try that? theDEVILjsb -10 — 6y
1
Computers cannot make actual random numbers, they just do some math (that I don't know) with the seed. To make it not repeating, set the seed, but the numbers still repeat when you call it enough. hiimgoodpack 2009 — 6y

3 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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:

  1. The algorithm used to generate random numbers is much better and produces "more random" results.
  2. The seed is scoped to the created generator instance. This solves a problem where setting the 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.

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

The other answers here aren't totally correct, unless you want to use the new random feature Roblox has added.

If you want something to be random, insert the following into the top of your script

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

Log in to vote
-1
Answered by 6 years ago

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

Answer this question