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.
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
local randomNumber = math.random(1,5)
local tableThing = {"Picked 1", "Picked 2"}
if randomNumber <= #tableThing then
print(tableThing[randomNumber])
end
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)
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.
Thank you for letting me know. I’ll just take the information provided here and thanks to @TheRadioactiveGear and @ItzMeZeus_IGotHacked for information too.
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