Math.radom with like 1.5 or 0.5

I’m trying to create a script wich math.randoms a number with like 1.5 or 0.5 I know I can divide it, but isnt there another way because then I also have to make the numbers even larger wich i don’t want. Please tell if there’s a way!

local moneyButton = script.Parent
local minValue = script.Parent.MinimumValue
local maxValue = script.Parent.MaximumValue
local npcDialogueFrame = script.Parent.Parent
local values = script.Parent.Parent.Parent.Parent:WaitForChild("Values")
local npcDialogueMoneyHandler = game:GetService("ReplicatedStorage"):WaitForChild("NpcDialogueMoneyHandler")
local moneyMultiplier = values.MoneyMultiplier

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local money = leaderstats:WaitForChild("Money")

moneyButton.MouseButton1Up:Connect(function()

local randomAmount = math.random(minValue.Value, maxValue.Value)
print(randomAmount)

local randomAmountMultiplied = randomAmount * moneyMultiplier.Value

print(randomAmountMultiplied)

npcDialogueMoneyHandler:FireServer(randomAmountMultiplied,money)

npcDialogueFrame.Visible = false

end)

Do You mean generating any random decimals (for example 1.2, 6.6, 8.5) or x.5, where x is an integer?

I mean numbers with decimals____

Is this what You are looking for? Yes, there’s still dividing but it is done only for the decimal part (one digit after decimal):
local randomDecimal = tonumber(math.random(1, 100) .. math.random(0, 9) / 10)

Put the minValue to 5 and maxValue to 15.

If you’re lazy, do this:

local moneyButton = script.Parent
local minValue = script.Parent.MinimumValue
local maxValue = script.Parent.MaximumValue
local npcDialogueFrame = script.Parent.Parent
local values = script.Parent.Parent.Parent.Parent:WaitForChild("Values")
local npcDialogueMoneyHandler = game:GetService("ReplicatedStorage"):WaitForChild("NpcDialogueMoneyHandler")
local moneyMultiplier = values.MoneyMultiplier

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local money = leaderstats:WaitForChild("Money")

moneyButton.MouseButton1Up:Connect(function()

-- If you're lazy to do it --
minValue.Value = 5
maxValue.Value = 15
-----------------------------

local randomAmount = math.random(minValue.Value, maxValue.Value)
print(randomAmount)

local randomAmountMultiplied = randomAmount * moneyMultiplier.Value / 10

print(randomAmountMultiplied/10)

npcDialogueMoneyHandler:FireServer(randomAmountMultiplied,money)

npcDialogueFrame.Visible = false

end)
4 Likes
local Value = math.random(10,20)/10

this will give a number from 1, 2 with decimals

I like using the Random object for this because there’s clearly distinct methods that allow you to generate either an integer or a float whereas math.random is really only kind to integers. The only differences you’d have to make in your code is creating the Random object and calling its randomiser.

-- Add this with your declarations at the top; feel free
-- to rename the variable.
local RNG = Random.new()

-- Replace math.random with a NextNumber call to the
-- Random object.
local randomAmount = RNG:NextNumber(minValue.Value, maxValue.Value)

This way, you make zero changes to your numbers (no performing arithmetics or changing your ranges) while gaining all the benefits provided by the Random object that math.random alone doesn’t offer such as localised entropy.

5 Likes