When using math.random on Sound's PlaybackSpeed, it's either 0 or 1

Hello devs! So I found issue with randomizing Sound’s PlaybackSpeed, when you for example want to set PlaybackSpeed of Sound to math.random(0.8,1.2) it sets PlaybackSpeed to 0 or 1 but not 0.8, 0.9, 1 etc.

1 Like

math.random() only work if you use ints, not floats. Try to do math.random(8, 12)/10

Edit: or math.random(80/120)/100 for more options

3 Likes

That didn’t work, but made issue worse, now it’s only 0

1 Like

Are there any errors in the output? Show the whole code.

1 Like

No errors, the code :

--Removed the code so it's private.

(Note that it’s script for Void Script Builder)

I said math.random(8, 12)/10 you need to use ints (integers) inside of this function (or math.random(80, 120)/100 for more options)

1 Like

Oh, didn’t notice, I’m fast reader.

Why not make your own custom random function?

function getRandomNumber(min,max)
   local numbers = {}
   for i = min,max,.1 do
      table.insert(numbers,i)
   end
   local choosenNumber = numbers[math.random(1,#numbers)]
   return choosenNumber
end

print(getRandomNumber(0,1)) -- Prints number between 0 to 1

Why should I reinvent the wheel?

Oops, you got the solution while I was typing :sweat_smile:

To add to this, you can use Random for an “unlimited” amount of choices, similar to what math.random() does without parameters.

local Rand = Random.new()
local function RandomNumber(Min, Max)
    return Rand:NextNumber(Min, Max)
end

Or alternatively remap math.random()'s range:

local function RandomNumber(Min, Max)
    local Diff = Max-Min
    return (math.random()*Diff)+Min
end

or math.random() * 0.4 + 0.8

1 Like