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

Help with math.random?

Asked by
Qorm 100
9 years ago

How to put math.random to switch between 2 seconds or 2.5 seconds or 2.75 seconds or 3 seconds in wait?

light=script.Parent.SpotLight
r=math.random(2,2.5),math.random(2.75,3)
rr=math.random(.3,.35),math.random(.4,.45),math.random(.5,.55)

The Output does "0 2 0 2 0 2 0 2 0 2" ECT.. ex: wait(r) --waits 2 or 2.5 or 2.75 or 3 seconds (random) How would I be able to do this without it doing the Output provided above? Help would be appreciated!

2 answers

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

math.random has two modes, only: integers and percentage

The integer mode will only give you integers (which is why you're only getting 0 and 2), and is used by putting in numbers into the function.

The percentage mode is simply: math.random() and will return a randomized, fractional number between 0 and 1, inclusive (although 0 and 1 are rare in and of themselves).

If you want to choose between 4 explicit steps randomly, use a table:

waits = {2, 2.5, 2.75, 3}
local r = waits[math.random(1, 4)]
print(r)
0
I'm fairly certain 1 is exclusive, just like with many other languages. jakedies 315 — 9y
0
Also the parenthesis should be brackets, but that was probably just a type. jakedies 315 — 9y
0
Yep, typo. Fixed it. I've had it get 1 before, though I was doing stupid things and it may have been a rounding fluke. adark 5487 — 9y
0
Even with rounding errors, maybe you used math.ceil? Regardless here's a quote from lua.org 'When called without arguments, returns a uniform pseudo-random real number in the range [0,1)' jakedies 315 — 9y
0
*shrug*. Like I said I was doing something stupid. The rarity of getting a 0 proves how rare a 1 would be if it even could appear. adark 5487 — 9y
Ad
Log in to vote
0
Answered by
Qorm 100
9 years ago
waits={0.35, 0.65, 1.35, 1.8}
waitss={0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5}
r = waits[math.random(1, 4)]
rr = waitss[math.random(1, 8)]

Output: 1.35 0.4 1.35 0.4 1.35 0.4 1.35 0.4 ECT... It seems that It's picking 2 random numbers from the tables to start with, then keeps using them over and over and over again.. How would I fix this? Full code:

light=script.Parent.light.SpotLight
baselight=script.Parent.light

waits={.35,.65,1.35,1.8}
waitss={.15,.2,.25,.3,.35,.4,.45,.5}
r = waits[math.random(1,4)]
rr = waitss[math.random(1,8)]

while true do
    print(r)
    print(rr)
    wait(r)
    light.Color=Color3.new(120,120,120)
    light.Angle=60
    light.Range=15
    light.Brightness=1
    baselight.BrickColor=BrickColor.new("Mid gray")
    wait(rr)
    light.Color=Color3.new(255,255,255)
    baselight.BrickColor=BrickColor.new("Light stone grey gray")
    light.Angle=71.5
    light.Range=20
    light.Brightness=3
end
0
NEVERMIND. I'm dumb. Sorry! Figured out to put it at the end Dx Qorm 100 — 9y

Answer this question