[SOLVED] Random Number Exceptions

Just a quick question, but is there anyway to make an exception to RandomInRange? For example, a random number in a range of (1,9), but I don’t want to pull an 8.

Not via the expression, no.

You could just set up your condition for that action to happen again if the variable you’re setting = 8.

So basically

Parent event conditions here | No actions
(Sub event 1) Trigger once | Set Variable to RandomInRange(1,9)
(Sub event 2) Variable = 8 | Set Variable to RandomInRange(1,9)
1 Like

If I were to go with that approach, I’d do it more like this:

<No condition>  |  Set RandomValue to 8
While:
  Value of RandomValue == 8
Do:
  Set RandomValue to RandomInRange(1,9)

This approach works, and does so especially well especially on big ranges with a small number of forbidden numbers.
If I have on the contrary a smaller range with a lot of forbidden values, I’d use this alternative approach:

  1. Make an array with all the authorized values once at the beginning of the scene
  2. To select a random authorized value, do Variable(TheArrayOfAuthorizedValues[RandomInRange(0, VariableChildCount (TheArrayOfAuthorizedValues)])

What’s good about this approach is that it has a constant time of execution, where the other approach may repeatedly generate a forbidden number and cause a momentarily lag every now and then.
What’s less good is that you have to build an array of authorized values, which is more tedious than the other method :confused:

4 Likes

Thanks for the replies! Everything worked well, and I went with @arthuro555 's while loop approach for anyone wondering, as I had a larger range to work with.

1 Like