NextInteger always returning same number, despite different inputs (ONLY ON MOBILE)

So as the title says, this problem is only seen on mobile, it works perfectly on PC
It also only happens if the seed(see code below) is negative, it works fine otherwise

I’ll explain more in a briefing
So this is a simplified version of the code:

-- This is a module script in ReplicatedStorage
-- These are example numbers
local Randomseed = -1000 -- Negative seed
local totalWeight = 15000

function module:Initiate(x, y) -- This mobule is always called with different arguments
	local random = Random.new(x + Randomseed .. math.abs(y + Randomseed))
	local RandomNumber = random:NextInteger(1, totalWeight)
	
	-- THE "RANDOMNUMBER" VARIABLE IS THE ONE WHICH IS ALWAYS THE SAME
	print(RandomNumber, "Coordinates: ", x, y)
end

I understand that Nextinteger is a pseudorandom number generator, but everytime it’s called, it’s called with a different x and y value(See script underneath and screenshots)

The script that calls the function numerous times:

-- This is a local script in StarterPlayerScripts, which calls the module script
local ModuleScript = require(game.ReplicatedStorage.ModuleScript)

local Range = 5

local function Activate()
	for x = -Range, Range do
		for y = -Range, Range do
			ModuleScript:Initiate(x, y)
		end
	end
end
Activate()

Now on pc, the “RandomNumber” Variable prints different everytime, on mobile, however, it prints the same number over and over again

Proof:
PC:


Mobile:

There is simply no reason for it not to work on mobile but work fine on pc, so i highly doubt this is on my side

REPRODUCTION STEPS:
The game: Roblox Bug Reports - Roblox
The game file: Roblox Bug Reports.rbxl (43.9 KB)
This happens on all games, 100% of the time (that is if the circumstances are correct, like negative seed and on mobile)
This has started since i implemented the code

If you want to make an empty baseplate yourself and test this, use the scripts above and the instructions below:

The module script must be in ReplicatedStorage
The activation script must be in StarterPlayerScripts

2 Likes

This is because Random.new internally behaves differently for negative numbers between Intel and Arm. On Arm, the seed is currently clamped to 0; on Intel it wraps around. You can observe this on mobile or on macOS M1/M2 so it’s not quite mobile specific.

We will fix this. For now you can work around this by using math.abs on the argument to Random.new so that you never pass negative numbers in.

6 Likes

Ohh, the fact that arm and intel work slightly differently with random.new explains a lot. Thank you all a lot for the help! :smiley:

1 Like

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