Math.random() not working as intended to

Hello everyone!

I have a daily reward system in my game. If a player has under 5000 Clicks, the reward you get will be "math.random(100, 5000). You will be rewarded in the “Clicks” currency. If the player has over 5000 Clicks, the you will be rewarded:

local divPlayer = player.leaderstats.Clicks.Value / 2
local mulPlayer = player.leaderstats.Clicks.Value * 2
randomNumber = math.random(divPlayer, mulPlayer)

This is what happens when I use the code above.

Please help!

Thanks

-GreenTreeGamingYT

Full Code:

local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local serverScriptService = game:GetService("ServerScriptService")

local remotes = replicatedStorage:WaitForChild("Remotes")

local manager = require(serverScriptService:WaitForChild("PlayerData"):WaitForChild("Manager"))

local map = workspace:WaitForChild("Map")

map.DailyChest.Touch.Touched:Connect(function(part)
	local player = players:GetPlayerFromCharacter(part.Parent)
	repeat wait() until player:GetDescendants()
	
	if player then
		if os.time() >= player.DailyReward.Value then
			local randomNumber
			if player.leaderstats.Clicks.Value <= 5000 then
				randomNumber = math.random(100, 5000)
			elseif player.leaderstats.Clicks.Value >= 5000 then
				local divPlayer = player.leaderstats.Clicks.Value / 2
				local mulPlayer = player.leaderstats.Clicks.Value * 2
				
				print(divPlayer)
				print(mulPlayer)
				print(math.random(divPlayer, mulPlayer))
				
				randomNumber = math.random(divPlayer, mulPlayer)
			end
			
			manager.AdjustClicks(player, randomNumber)
			player.TotalClicks1.Value += randomNumber
			
			player.DailyReward.Value = os.time() + 21600
			remotes.DailyReward:FireClient(player, randomNumber)
		else
			remotes.DailyReward:FireClient(player)
		end
	end
end)

map.GroupChest.Touch.Touched:Connect(function(part)
	local player = players:GetPlayerFromCharacter(part.Parent)
	repeat wait() until player:GetDescendants()
	
	if player then
		if player:IsInGroup(15197958) then
			if os.time() >= player.GroupReward.Value then
				local randomNumber
				if player.leaderstats.Clicks.Value <= 5000 then
					randomNumber = math.random(100, 5000)
				elseif player.leaderstats.Clicks.Value >= 5000 then
					local bottomNum = math.floor(player.leaderstats.Clicks.Value / 2)
					local topNum = math.floor(player.leaderstats.Clicks.Value * 2)
					
					randomNumber = math.random(bottomNum, topNum)
				end
				
				manager.AdjustClicks(player, randomNumber)
				player.TotalClicks1.Value += randomNumber

				player.GroupReward.Value = os.time() + 21600
				remotes.GroupReward:FireClient(player, randomNumber)
			else
				remotes.GroupReward:FireClient(player)
			end
		else
			remotes.GroupJoin:FireClient(player)
			remotes.GroupReward:FireClient(player)
		end
	end
end)

In simple terms, math.random can only handle values between -231 and 231-1 as any values outside of this range is an undefined behaviour which happened in your case. (how the undefined behaviour actually behaves commonly, such as why does it get casted to -2147483648, will almost certainly be confusing for you given that you posted this.)

The NextInteger method of Random object can handle much larger value so I’d recommend using that instead.

Thank you @Blockzez! Worked like a charm.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.