# Is there any better way to do events then math.random?

So when I do random stuff, I usually just do elseif with math.random and I think this is inefficient way to do things but I’m not sure how or what would be better way.

For example;

``````local RandomNumber = math.random(1,5)

if RandomNumber == 1 then
print("yes")
elseif RandomNumber == 2 then
print("no")
end
--And so on.
``````

Any idea how to make this better? Maybe use of some modules to do random events.

Why is it inefficient? I’ve always used it for choosing random things and it has always done its job.

Don’t think so, but math.randomseed() can affect the randomness.

``````local RandomNumber = math.random(1,5)

if RandomNumber == 1 then
print(RandomNumber, 1)
print("Printed 1")
elseif RandomNumber == 2 then
print(RandomNumber, 2)
print("Printed 2")
end
``````

For something like that, I would use an array.

``````local randomNumber = math.random(1,5)
local tableThing = {"Picked 1", "Picked 2"}

if randomNumber <= #tableThing then
print(tableThing[randomNumber])
end
``````

Not 100% sure if this works though.

Not sure, I just always thought it looked messy and wasn’t too sure if there was a better way or something. Especially if you have many as math.random(1,40)

add math.randomseed(tick()) or math.randomseed(os.time()) at the top for more randomness

just a more explained version of @ItzMeZeus_IGotHacked 's reply

1 Like

Unfortunately, there isn’t a simple way to do this really, maybe you could keep the strings in a module and just call them from there, but a simple answer would be that as far as I know, there is no easier way to do this, even good programmers do this so don’t worry about it not looking too good, other than that, if someone has another way, please correct me but even I still do this so don’t be worried.

1 Like

Thank you for letting me know. I’ll just take the information provided here and thanks to @TheRadioactiveGear and @ItzMeZeus_IGotHacked for information too.

1 Like

The reason it’s inefficient is because it’s pretty tough to edit and modify. A new event means an additional else if statement which programmers are usually lazy to do though it may be fine a “small” number of events I would recommend using a weighted table some thing like this, that way you can keep adding events and do something like a 2x chance event for a certain item easily by multiplying the weight s by two