Math.random doesn't seem very random

I’m trying to make a npc automatically say something, but the randomness of it doesn’t feel right.

I’ve coded it so that each second, a variable is randomly changed to a value between 1 and 32, and that is it’s a certain number then it will say something, but the issue is it’s saying it a lot more then it theoretically should be. Despite it having a 1 in 32 chance of saying something, it says it every few seconds as if it was 1 in 3 or 4

I’ve tried making it wait a certain amount of time before saying something, but that means that it’s a matter of when it will say it. The dialogue is supposed to be a secret hint to a side quest, so I would much prefer if it was a matter of if it will say it.

Here’s my code, can someone tell me what I’ve done wrong?

while true do
	local random = math.random(1,32)
	if random == 16 then
		game:GetService("Chat"):Chat(script.Parent.Head, "dialogue 1")
	end
	
	local random2 = math.random(1,200)
	if random2 == 100 then
		game:GetService("Chat"):Chat(script.Parent.Head, "secret hint dialogue")
	end
	wait(1)
end

You didnt do anything wrong, this is how computers work. Computers can’t be random, at least not digital ones (Quantum ones might). So a computer to be “random” it takes exterior factors, for example the weather, the runtime, the inputs, the day, the date etc… To generate the random number.

An example of this in action was a Pokémon game, the original one had a problem since it only used the player inputs as the random factor, that means that if the player pressed the exact same combination before playing, it will give the world it wanted. Later Pokémon made so that it takes into account date etc. and made it harder to replicate.

There are methods to increase randomness, but I don’t recommend them, it has to do with the math.randomseed.

3 Likes

By the logic of how you implemented the code. It honestly feels like it does it’s job as it should. You can even confirm this by doing a simple print(random) statement to let you know what outputs are being randomly generated.

Just like what @STORMGAMESYT7IP, computers aren’t actually random.

A workaround for your problem is to increase the second number inside the math.random() function. Try adding more zeros to it until you feel that it’s rare enough.

2 Likes

I’ve just tried that as after reading his comment I figured it would work, and it kind of has. Thanks for the help

1 Like