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

Not creating random numbers?

This Code isn't creating random numbers/outputs

local b1 = mob.B1
		math.randomseed(tick())
	    p1 = math.random(1, 100)
	    if p1 <= b1.None.Value then
		elseif p1 > b1.None.Value and p1 <= b1.Crude.Value then
		print("Crude for b1")
		elseif p1 > b1.Crude.Value and p1 <= b1.Rare.Value then
		print("Rare for b1")
		elseif p1 > b1.Rare.Value and p1 <= b1.Famed.Value then
		print("Famed for b1")
		elseif p1 > b1.Famed.Value and p1 <= b1.Legendary.Value then
		print("Legendary for b1!")

2 comments

0

math.randomseed doesn't work in studio.

0

I think that depends on your operating system, Ruben. Discarding the first couple of generated values and using the subsequent ones seems to solve the problem.

2 answers

✓ Accepted answer

Here is my solution:

math.randomseed(os.time())
-- NOTE I USED OS.TIME, NOT TICK!
math.random();
math.random();

I think this is related to a question from a few days ago: Random always picks last element in table?

My answer there was:

Yikes! You've stumbled on a whopper. Have a look at this question on StackOverflow.

Basically, the ROBLOX version of Lua relies on a cross-platform C implementation of rand(3), which, if you're on OSX [or Windows 10], isn't the best generator.

I tested this on OSX and, you're right, the first generated number is the same regardless of the seed! To mitigate this, call math.random() a couple of times before you actually use the output - the rest of the numbers are random, just not the first one.

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