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

How do I do math.random for Decimals?

local function HoverSound()
	local Pitch = math.random(0.9, 1.2)
	script.Click.PitchShiftSoundEffect.Octave = Pitch
	script.Click:Play()
end

I know math.random doesn't work, but I can't find what works on the wiki. Thanks for your help.

2 answers

Instead of using math.random, you can use the newer Random object that roblox introduced. https://developer.roblox.com/en-us/api-reference/datatype/Random

To use it for your example you would do this:

-- Create a new random object
local rng = Random.new()

local function HoverSound()
	local Pitch = rng:NextNumber(0.9, 1.2)
	script.Click.PitchShiftSoundEffect.Octave = Pitch
	script.Click:Play()
end

1 comment

0

You could do that, or if you don't want to stop using math.random you can just divide it.

Eh just simple math. Just divide or multiply with a multiple of ten after the randomized number.

local a = math.random(1,10); --// Range: 1 to 10
a = a / 10; --// New range: 0.1 to 1
print(a); 
a = a * 100 --// New range: 10 to 100
print(a)

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