Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I make this random number generator more random?

The problem with my current system is that a pattern occurs when the player first rolls the dice, and then doesn't change until they change the number of sides of the dice.

For example, when I first load the game, the game defaults to 1d6. So when I roll the dice, let's say that the outcome was 6. No matter how many times I roll the dice after that, or how many dice I roll, the result is ALWAYS 6 until I change the sides, as you can see below.

sides = 6
numdice = 1
rolls = {}


function rollthedice()
	for i = 1, numdice do
		math.randomseed(tick())
		table.insert(rolls, math.random(1,sides))
	end
	print("Rolled "..numdice.." dice with "..sides.." sides!")
	print(table.concat(rolls, ' '))
end

And here's my output:

Rolled 1 dice with 6 sides! 6 Rolled 3 dice with 6 sides! 6 6 6 6 Rolled 3 dice with 14 sides! 6 6 6 6 13 13 13 Rolled 1 dice with 14 sides! 6 6 6 6 13 13 13 13 Rolled 1 dice with 14 sides! 6 6 6 6 13 13 13 13 13 Rolled 1 dice with 14 sides! 6 6 6 6 13 13 13 13 13 13

Obviously I'd like to eliminate these patterns, if at all possible. I want this to be truly random, or as close as I can easily and feasibly get.

1 answer

✓ Accepted answer

You're misusing math.randomseed.

math.randomseed should only be called once in your script -- calling it destroys the randomness that has been built-up.

Move it to be at the very top, only.

I'm not sure that calling it directly with tick() is the best idea, because tick() produces a very large number. tick() % 1e7 or tick() % 1 * 1e7 might work better, though I'm not sure.

This discussion is preserved as it was. New answers, comments, and votes are closed.