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

Not creating random numbers?

Asked by 7 years ago

This Code isn't creating random numbers/outputs

local b1 = mob.B1
        math.randomseed(tick())
        p1 = math.random(1, 100)
        if p1 <= b1.None.Value then
        elseif p1 > b1.None.Value and p1 <= b1.Crude.Value then
        print("Crude for b1")
        elseif p1 > b1.Crude.Value and p1 <= b1.Rare.Value then
        print("Rare for b1")
        elseif p1 > b1.Rare.Value and p1 <= b1.Famed.Value then
        print("Famed for b1")
        elseif p1 > b1.Famed.Value and p1 <= b1.Legendary.Value then
        print("Legendary for b1!")
0
math.randomseed doesn't work in studio. RubenKan 3615 — 7y
0
I think that depends on your operating system, Ruben. Discarding the first couple of generated values and using the subsequent ones seems to solve the problem. duckwit 1404 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago

Here is my solution:

math.randomseed(os.time())
-- NOTE I USED OS.TIME, NOT TICK!
math.random();
math.random();
Ad
Log in to vote
1
Answered by
duckwit 1404 Moderation Voter
7 years ago

I think this is related to a question from a few days ago: Random always picks last element in table?

My answer there was:

Yikes! You've stumbled on a whopper. Have a look at this question on StackOverflow.

Basically, the ROBLOX version of Lua relies on a cross-platform C implementation of rand(3), which, if you're on OSX [or Windows 10], isn't the best generator.

I tested this on OSX and, you're right, the first generated number is the same regardless of the seed! To mitigate this, call math.random() a couple of times before you actually use the output - the rest of the numbers are random, just not the first one.

Answer this question