Math.random() under 1 doesn't work

Not sure if this is suppose to be a “bug report”, but whenever i call math.random to generate a number below 1 (i.e math.random(0.7,0.9)) it always returns 0. I’m trying to make a coin gui that goes up the screen whenever u get a coin. heres what i have

local SoundService = game:GetService("SoundService")
repeat wait() until Client:FindFirstChild("leaderstats") and Client.leaderstats:FindFirstChild("Coins")

local lastval = Client.leaderstats.Coins.Value
Client.leaderstats.Coins:GetPropertyChangedSignal("Value"):Connect(function()
	local newval = Client.leaderstats.Coins.Value
	if newval > lastval then
		local change = newval - lastval
		
		local ScreenGui = script.AddCoinsFrame:Clone()
		local MainFrame = ScreenGui.MainFrame
		ScreenGui.Parent = Client.PlayerGui
		local x = math.random(0.1,0.9)
		MainFrame.Position = UDim2.new(x,0,math.random(0.7,0.9),0)
		MainFrame.Amount.Text = "+"..change
		SoundService:PlayLocalSound(game.ReplicatedStorage.Client.Sounds.CoinCollect)
		local t = 1
		MainFrame:TweenPosition(UDim2.new(x,0,math.random(0.1,0.3),0),Enum.EasingDirection.In,Enum.EasingStyle.Sine,t)
		wait(t)
		ScreenGui:Destroy()
		
	end
	
	lastval = newval
end)
1 Like

You can use Random:NextNumber in place of that.

local rand = Random.new(seed)
rand:NextNumber(0.7, 0.9)
1 Like

Math.random(m, n) only returns integers. To get a random double in the range, you have to do math.random(70,90) / 100

3 Likes

What should i put as the seed?
EDIT: I figured it out, its optional to put the seed there

The seed is optional, just incase you’d ever want to use one. I forgot if one is automatically picked if not provided, but it will be a determinant which decides the sequence of numbers produced by :NextNumber. The same seed will return the same numbers in the same order every time, so it may be useful for debugging.

1 Like

For anyone thats reading in the future :slight_smile:

randomInt=function(...)return Random.new():NextNumber(...)end
print(randomInt(0.7,0.9)) -- 0.73039298444162

However calling math.random() with no arguments returns a number from 0 - 1, not necessarily an integer.

2 Likes