Math.randomseed keep returning to 7 after givng it a high x parameter

As the title says, every time I insert a big amount of digits in the math.randomseed and printing a random number between 0 to 100, it keeps returning “7”.

Using small digits like 6 digits will return normally without any issues.

I tested it in a different blank script it still gave the same problem.

Here’s my code:

math.randomseed(112111111111)
print(math.random(1, 100)) -- 7

math.randomseed(11214411161532183111)
print(math.random(1, 100)) -- 7

math.randomseed(61361234124312)
print(math.random(1, 100)) -- 7
1 Like

I did a little test because I was curious:

local Power = 10
local NumberToAdd = 10

for _ = 1, 100 do
	repeat
		Power += NumberToAdd
	until (function() 
		math.randomseed(2 ^ Power) 

		return math.random(1, 100) == 7
	end)()
	
	NumberToAdd /= -2
end

print(Power, 2 ^ Power)

Output: 36.66666666666666 109085369661.4299

Looks like if your number is 2 ^ 36.666666... or more on the first parameter of math.randomseed, it will return the same number if you use math.random.

Alright I edited this post too much but incase you don’t know what is happening, I’m getting the limit of math.randomseed

EDIT:

Alright, so to avoid false results, I made the second parameter bigger in math.random so there’s an impossible chance to get false results:

local Power = 10
local NumberToAdd = 10

local function RandomedNumberWithSeed(Seed, RandomMin, RandomMax)
	math.randomseed(Seed)
	
	return math.random(RandomMin, RandomMax)
end

for _ = 1, 100 do
	repeat
		Power += NumberToAdd
	until RandomedNumberWithSeed(2 ^ Power, 1, 2 ^ 25) == RandomedNumberWithSeed(math.huge, 1, 2 ^ 25)
	
	NumberToAdd /= -2
end

print(Power, 2 ^ Power)

Still the same results (36.66666666666666 109085369661.4299)
Maybe some technical gods can answer? Because I have no clue now, I searched the number in Google and no results, did I do something wrong? :sob:

I tried your code and I got the same result as yours.
Since I’m stupid at maths, I benchmarked the maximum randomseed parameter until it returns to 7, manually.

Here’s the code:

math.randomseed(123456)
print(math.random(1, 100)) -- normal

math.randomseed(1234567)
print(math.random(1, 100)) -- normal

math.randomseed(12345678)
print(math.random(1, 100)) -- normal

math.randomseed(123456789)
print(math.random(1, 100)) -- normal

math.randomseed(1234567891)
print(math.random(1, 100)) -- normal

math.randomseed(12345678912)
print(math.random(1, 100)) -- 7

math.randomseed(123456789123)
print(math.random(1, 100)) -- 7

Apparently, after 10 digits it’s started returning 7.
However, when I increase the maximum value of math.random to 1000, it returns 68 now.

math.randomseed(12345678912)
print(math.random(1, 100)) -- 7

math.randomseed(12345678912)
print(math.random(1, 1000)) -- 68

math.randomseed(12345678912)
print(math.random(1, 10000)) -- 676

I’m literally confused rn why it happens.

1 Like

Anyways, thank you so much for replying. I really appreciate it.

I gave up trying to find the problem about the math.randomseed but I came out with a different method to seed a random number without making the first parameter of the seed higher by dividing them.

EDIT: I was wrong.

Luckily while I’m experimenting with the isusse, I just found something what’s more interesting.
The reason why I’m getting the 7 as the returned value is that I’m keep repeating the same math.randomseed function on each line I run a math.randomf from 0 to 100.

When I fixed everything, it works… (kind of)

When the parameters reach more than 10 digits, it keeps repeating the same sequence no matter how many times you change the seed or increase it.

-- Original Seed:
math.randomseed(111111111111)
print(math.random(1,100)) -- 7
print(math.random(1,100)) -- 1
print(math.random(1,100)) -- 15
print(math.random(1,100)) -- 69
print(math.random(1,100)) -- 89
print(math.random(1,100)) -- 60
print(math.random(1,100)) -- 61
print(math.random(1,100)) -- 30

-- With a Different Seed:
math.randomseed(222222222222)
print(math.random(1,100)) -- 7
print(math.random(1,100)) -- 1
print(math.random(1,100)) -- 15
print(math.random(1,100)) -- 69
print(math.random(1,100)) -- 89
print(math.random(1,100)) -- 60
print(math.random(1,100)) -- 61
print(math.random(1,100)) -- 30

However, when I lowered down the digits, it runs perfectly normal as to how the math.randomseed works.
I guess I’ll have to find a way to seed a random number without reaching the maximum limit.

Look’s like it’s going to be a mystery for now. It probably has to do with the algorithm it used.

1 Like

I stole @imKirda’s script, modified it a little, and made this:

local StarterGui = game:GetService("StarterGui")

local screenGui: ScreenGui = Instance.new("ScreenGui")
screenGui.IgnoreGuiInset = true
screenGui.ResetOnSpawn = false
screenGui.Name = "Root"

local backgroundFrame: Frame = Instance.new("Frame", screenGui)
backgroundFrame.BackgroundColor3 = Color3.new(0.5, 0.5, 1)
backgroundFrame.BorderSizePixel = 0
backgroundFrame.Size = UDim2.fromScale(1, 1)
backgroundFrame.Name = "Background"

local function RandomedNumberWithSeed(Seed, RandomMin, RandomMax)
	math.randomseed(Seed)

	return math.random(RandomMin, RandomMax)
end

local function drawLine(from: Vector2, to: Vector2): Frame
	local line: Frame = Instance.new("Frame")
	line.BackgroundColor3 = Color3.new(0, 0, 0)
	line.BorderSizePixel = 0
	line.AnchorPoint = Vector2.one / 2
	line.Position = UDim2.fromOffset((from.X + to.X) / 2, (from.Y + to.Y) / 2 + 64)
	line.Rotation = math.deg(math.atan2(to.Y - from.Y, to.X - from.X))
	line.Size = UDim2.fromOffset(math.sqrt((from.X - to.X) ^ 2 + (from.Y - to.Y) ^ 2), 2)
	line.Name = "Line"

	return line
end

local previousPosition: Vector2

for index: number = 1, 4000 do
	local frame: Frame = Instance.new("Frame", backgroundFrame)
	frame.BackgroundTransparency = 0
	frame.BackgroundColor3 = Color3.new(0, 0, 0)
	frame.BorderSizePixel = 0
	frame.AnchorPoint = Vector2.yAxis
	frame.Position = UDim2.new(0, index * 0.25, 0.5, -RandomedNumberWithSeed(math.huge, 1, index))
	frame.Size = UDim2.fromOffset(4, 4)
	frame.Name = tostring(index)

	local frameCorner: UICorner = Instance.new("UICorner", frame)
	frameCorner.CornerRadius = UDim.new(0.5, 0)

	--if previousPosition then
	--	local line: Frame = drawLine(previousPosition, frame.AbsolutePosition)
	--	line.Parent = backgroundFrame
	--end

	-- If I did draw lines for the dots my computer would explode because you would need 5999 frames
	-- And it's so unnecessary because the precision of the frame is so tight

	previousPosition = frame.AbsolutePosition
end

screenGui.Parent = StarterGui

You do not need to understand the full script, but this is a chart of different second parameter of the function math.random (I explained this part poorly, sorry). If you want to use the script, put it in ServerScriptService as a Script and paste in the code in. This is what it will look like after you do all the steps and run the code:

The results are pretty linear, so I tried to make a formula for it, basically, to calculate how you would get the “weird number 7” in the math.random(1, 100) function, you would do need to use this formula:

math.round((randomparameter2 - randomparameter1) / 15)
Doesn’t work 100% of the time, but it works pretty well

math.round((100 - 1) / 15) equals to 7
math.randomseed(math.huge) would make the function math.random(1, 100) equals to 7, see the connection?

math.round((600 - 1) / 15) equals to 40
math.randomseed(math.huge) would make the function math.random(1, 600) equals to 41, although it’s not the equaled value to the math.round((600 - 1) / 15) as I mentioned above, it’s pretty near

Don’t know how would this add to the conversation, but maybe this would give other people some clues of what is the algorithm, or is it an engine bug?

Look like I have overcomplicated so many things, please don’t complain (6/5/2022)

1 Like

I see about the formula, but what’s weirder is that it only happens when the digits are high. I’m slowly starting to assume that it’s a bug, or it might be due to how the algorithm works inside the randomseed function.

The description of the randomseed does not explain a lot, but it may be a clue to how it really works.

I’m still impressed about how the randomseed contains a math.huge parameter inside the script you modified.

and again, thank you so much for sharing your knowledge!
I apologize for not being useful that much.

1 Like

The max number you can use in math.randomseed() is the 32 bits integer “2147483647”

2 Likes

Oh, I see! I guess I’ll have to reduce my seed integer.
Thank you!

1 Like