Page 1 of 1

Using math.random with another variable instead of a number

Posted: Fri Nov 13, 2020 5:49 pm
by FlaconiaUnited
Hey guys. We are trying to make a randomized character creator, but running into some problems. It is set up as follows.

We begin with a starting variable: StatBudget, which begins at 30. The aim is to have 3 stats that all together add up to 30.

STR is generated using math.random(1,20).
StatBudget is then set to StatBudget - STR.
WIS should be generated by picking a random number between 1 and StatBudget.
StatBudget is then set to StatBudget - WIS.
CHA will then be set to the remaining StatBudget number.

However, our code for generating WIS is not working. This is what we have currently in the script field:

Variable["Wisdom"] = math.random(1, (Variable["StatBudget"]));

Is it possible to use math.random in this way, by replacing a number with another variable? Hopefully we are just doing it wrong. :D

Re: Using math.random with another variable instead of a number

Posted: Fri Nov 13, 2020 7:52 pm
by Tony Li
Hi,

Yes, that's fine to do.

You may find it helpful to temporarily add a Lua Console to your scene so you can type in those Lua statements manually to make sure your syntax is correct. For example, I just typed this in to test:

Code: Select all

Variable["StatBudget"] = 20
Variable["Wisdom"] = math.random(1, Variable["StatBudget"])
return Variable["Wisdom"]
return Variable["StatBudget"]
I typed each line individually. Some don't return any output, but the last two [that start with 'return'] return reasonable values. I also entered just "return Variable" to print out the values of all variables.