NextInteger always returning same number, only seen on mobile

So i’m making an infinite version of 3008, and it works perfectly, however, when i play on mobile, i see that everywhere i go it spawns the same exact plot. I only see this on mobile, not on pc.

It only happens when a plot is placed in a negative coordinate. So everything is normal if it’s a positive.

This is the ‘formula’ i’m using, where x and y are the coordinates of the plot, which is added with a random seed, so that the map is different every time you play

-- Full script later in this post
local Random = Random.new(x + Randomseed..math.abs(y + Randomseed))
local randomNumber = Random:NextInteger(1, totalWeight)

This is what that issue looks like:

I have looked on the devforum for answers, and I only learned that this is acutally roblox’s fault, and that I can’t acutally fix it. But that won’t fix the problem.

If there are any questions i will try to answer them

Game: 3008 Infinite - Roblox

EDIT:
To clear up some confusion, i will explain the entire script:

Loot table Script
function PlotService:GetRandomItemFromLootTable(lootTable, x, y)
	Randomseed = require(ReplicatedStorage.Randomseed).Seed
	local totalWeight = getTotalWeightOfLootTable(lootTable)

	local random = Random.new(x + Randomseed..math.abs(y + Randomseed))
	local randomNumber = random:NextInteger(1, totalWeight)
	
	-- The randomNumber variable is used here
	for _, entry in ipairs(lootTable) do
		if randomNumber <= entry.Weight then 
			return entry.Model
		else 
			randomNumber = randomNumber - entry.Weight
		end
	end
end
Loot table
local PlotWeights = {
	["Plots"] = {
		{Model = PlotModels[1], Weight = 4000}, 
		{Model = PlotModels[2], Weight = 5000}, 
		{Model = PlotModels[3], Weight = 5000},
		{Model = PlotModels[4], Weight = 3500},
		{Model = PlotModels[15], Weight = 4000},	
		--40%, 17500 Weight
		
		{Model = PlotModels[5], Weight = 4000},
		{Model = PlotModels[6], Weight = 3500},
		{Model = PlotModels[7], Weight = 2000},
		{Model = PlotModels[8], Weight = 3625},
		--30%, 13125 Weight
		
		{Model = PlotModels[9], Weight = 2600},
		{Model = PlotModels[10], Weight = 2800},
		{Model = PlotModels[11], Weight = 3400},
		{Model = PlotModels[12], Weight = 2025},
		{Model = PlotModels[13], Weight = 2300},
		--30%, 13125 Weight
	}
}

Keep in mind that everything in the loot table is sorted later in the script

I’m using a loot table system, to make some plots are rarer than others, so i need a random number that is between 1 and the total amount of weight, which is then compared with the weights you see in the loot table

Are you creating a new Random.new with the same speed everytime you make a number? If that’s the case, then that’s expected behaviour.

Random numbers in computing are pseudo random and use the perlin noise algorithm. Make sure you only create it once to prevent that.

2 Likes

No, everytime a plot needs to be placed, it calls this function, with the x and y coordinates
Snippet:

function GetRandomPlot(lootTable, x, y)
-- A plot needs to be placed at, lets say, 10, 10
	Randomseed = -1000 -- let's say it's a negative seed
	local totalWeight = 1000 -- let's say a 1000

	local random = Random.new(x + Randomseed..math.abs(y + Randomseed))
	local randomNumber = random:NextInteger(1, totalWeight)
	
	-- Then it will choose a plot, with the randomNumber
end

The idea is, if you found a rare plot of some kind, you can walk a 1000 studs, and if you walk back, it will be the same plot you found earlier, with the same plots around it.

Thats why i’m using nextinteger, so it’s the same everytime, but what’s not the same is the coordinates, x and y, which are always different. So it’s weird that it chooses the same plot everytime despite the different coordinates

1 Like

Random.new() takes a seed for a parameter that means it will return the same random number.

2 Likes

It’s like generating a world in minecraft, every world has a seed so the player can load that exact world. But everything is still random.

2 Likes

Yeah that was the idea of this game

1 Like

This is kinda funny. I have been planning to make a 3008 game myself for a WHILE! So Lol I’ll try not to copy any of your ideas. I don’t like taking others work. I will make things much different from this one. :crazy_face: :man_facepalming:

2 Likes
local X, Y
local seed = 0 -- Whatever Number
local random = Random.new(seed)
X = random:NextNumber(100, 200)
Y = random:NextNumber(100, 200)

??? 1, 2

1 Like

Well I don’t mind if you use my ideas, go ahead if you want :smiley:!

2 Likes

I dont really get what you’re trying to do here.
My ‘random’ variable has two inputs, x and y which i do not see here(?)

1 Like

Ok. :slightly_smiling_face: I’m still not gonna plagiarize your work.

2 Likes

You probably want to use x and y for the random inputs

1 Like

Im starting to think that this is Roblox’s issue, because there is literally no reason that this doesnt work on mobile, when it works fine on pc.

1 Like

Have you considered coding your own random number generator?

Here is an example from ChatGPT (tested):

local MT = {}
local index = 313

function initialize(seed)
	index = 313
	MT[0] = seed
	for i = 1, 311 do
		MT[i] = bit32.band(636413622 * bit32.bxor(MT[i - 1], bit32.rshift(MT[i - 1], 30)) + i, 0xFFFFFFFF)
	end
end

function extract_number()
	if index >= 312 then
		generate_numbers()
	end

	local y = MT[index]
	y = bit32.bxor(y, bit32.rshift(y, 29))
	y = bit32.bxor(y, bit32.band(bit32.lshift(y, 17), 0x71d67fff))
	y = bit32.bxor(y, bit32.band(bit32.lshift(y, 37), 0xfff7eee0))
	y = bit32.bxor(y, bit32.rshift(y, 43))

	index = index + 1
	return bit32.band(y, 0xFFFFFFFF)
end

function generate_numbers()
	for i = 0, 311 do
		local y = bit32.bor(bit32.band(MT[i], 0x80000000), bit32.band(MT[(i + 1) % 312], 0x7fffffff))
		MT[i] = bit32.bxor(MT[(i + 156) % 312], bit32.rshift(y, 1))

		if y % 2 ~= 0 then
			MT[i] = bit32.bxor(MT[i], 0xB5026F5A)
		end
	end
	index = 0
end

function random_num(seed, min, max)
	initialize(seed)
	local range = max - min
	local random_number = extract_number()
	return min + (random_number % (range + 1))
end

-- Example usage:
for i = 1, 5 do
	print(random_num(i, 1, 100))
end

3 Likes

I will try this solution after i get back from vacation, thanks for the help!

EDIT:
Thanks i got it to work!

I had to make some additional changes tho

	math.randomseed(Randomseed) -- only affects 'MultiplyFactor'
	local MultiplyFactor = (math.random() + 0.001) * 100000
	local SEED = math.noise((x+Randomseed)/10, (y+Randomseed)/10, Randomseed / 10) * MultiplyFactor
	-- I had to combine X and Y for the seed, which is done above
	local randomNumber = random_num(SEED, 1, totalWeight)

To combine the x and y values i had to use perlin noise

Even though this works fine, you can still spot repeating plots here and there:

1 Like

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