Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with math.random?

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

✓ Accepted answer

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)

5 comments

0

I'm fairly certain 1 is exclusive, just like with many other languages.

0

Also the parenthesis should be brackets, but that was probably just a type.

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.

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

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.

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

1 comment

0

NEVERMIND. I'm dumb. Sorry! Figured out to put it at the end Dx

This discussion is preserved as it was. New answers, comments, and votes are closed.