Using the Feigenbaum Constant to generate random numbers

So most people may be wondering what on earth is the Feigenbaum Constant. A very great youtuber that I love to watch can sum up exactly what it is if you watch this video: https://www.youtube.com/watch?v=ETrYE4MdoLQ

Great! You watched the video, now the fun part begins!

As you know by taking λ * ans * (1 - ans) where λ = 4 and the answer is between 0-1 you will get a new number that follows next in the pattern. Using 4 as the λ is useful as it gives tons of different results before the pattern restarts. This is exactly what we want because it would be near impossible for someone to guess the pattern in a small amount of time.

local math = setmetatable({}, {__index = math})
local seed

local function randomSeed()
	local guid = game:GetService('HttpService'):GenerateGUID(false)
	local seed = ''
	for i = 0,#guid,4 do
		local section = guid:sub(i,i+4)
		local numb = 0
		for ii in section:gmatch('.') do
			numb = numb + ii:byte()
		end
		seed = seed .. numb
	end
	return '0.'..seed:sub(3,#seed)
end
seed = randomSeed()

function math.random(...)
	local numb = 4 * seed * (1-seed)
	seed = numb
	
	local args = {...}
	
	if #args == 1 then
		if not tonumber(args[1]) then return error('bad argument #1 to \'random\' (number expected, got '.. type(args[1]) ..')') end
		return math.ceil(numb * args[1]);
	elseif #args == 2 then
		if not tonumber(args[1]) then return error('bad argument #1 to \'random\' (number expected, got '.. type(args[1]) ..')') end
		if not tonumber(args[2]) then return error('bad argument #2 to \'random\' (number expected, got '.. type(args[2]) ..')') end
		return math.ceil(numb * (args[2] - (args[1]-1))) + (args[1]-1);
	end
	
	return numb
end

return math

This simple module I created puts the Feigenbaum Constant to use as shown in the math.random function I created.

The randomSeed function above the math.random function uses the guid generator to create a somewhat random seed which is what the math.random function will use to run off of. (Theoretically you don’t need to have the randomSeed function as that function is primarily used to make sure the pseudo random pattern is different each time the server starts.)

Edit: You don’t need the randomSeed function, that was a function I created so that I didn’t have to apply a random seed every time to the code so the generated pattern can always be some what random. This whole concept is to show how you can generate random numbers with the Feigenbaum Constant.

15 Likes

Just seems like it’s generating random numbers based off a pre generated guid. I don’t see any reason to use this over the built in function as well.

I believe it’s more of an insight into generating random numbers as apposed to a practical function to be used over math.random().

I’d highly recommend videos from Numberphile, Tom Scott, etc though as they provide a great insight into a range of computer science related applications. :+1:

3 Likes

That video is awesome! Super neat you can generate a random number with such simple code.

5 Likes

Pretty sure this is just a scripting exercise. Nobody’s expected to actually use this in production code, but it’s a good way to introduce psuedorandom number algorithms!