Question about random chances?

I need help getting random events to trigger.

I have an “if” turn script set up to go off at the end of every turn. It has a “if” 70% chance for something to happen, “else if” 25% for another event and “else if” 5% for a rare event. I figured since this adds up to 100% something would happen at the end of every turn.

It works sometimes but then sometimes nothing happens. Any ideas why???

I can only guess without seeing your code, that you aren't giving it a path for every possible outcome of the RNG method you are using. You are possibly checking for a 70, a 25, and a 5 for the chance, but testing the three outcomes instead of one.

Be sure to supply your script. There are many ways to program one thing and you are asking us about something that is programmed wrongly... and there are infinite ways to do it wrongly. Throw that code bone, yo.

if (RandomChance(25)) {
msg ("The monster’s attack misses.")
else if (RandomChance(70)) {
msg ("The monster’s claws dig into you, dealing damage.")
player.Health = player.Health-(Monster.Damage-player.Armour)
else if (RandomChance(5)) {
msg ("You suffer a devastating critical hit that shatters your resolve! You gain 15 stress.")
player.Health = player.Health-((Monster.Damage*2)-player.Armour)
player.Stress = player.Stress+15

Here is code. I think problem is the “else ifs”. The outcome is mostly the 70% one so when it’s not the game thinks there is no other outcome. Should these be “else” instead.

Actually the problem is the RandomChance function (or the way you have used it at least)
The way you have it currently, each time you use it it generates a new random number so it is quite possible that it will "miss" each chance when it is tested.
You need to generate the random number and store it in a variable so that the same number can be tersted at each step.
like this psuedocode...

mychance = RandomChance (100)
if mychance < 5 then special case message
else if mychance < 25 then miss message.
else hit msg. 

Then you need to test the value in order from smallest to largest so that you give the appropriate message.
Hope you can understand this. I am used to BASIC but for this it is very similar.
Good luck.

There is a good example in the documentation using the switch statement.

Thanks for the responses!

Makes sense - I don’t have the random chance stored as it’s own object/variable so I will give that a try and report back later :)

You could also adjust the chances of making it work.

If you have a block like this:

if (RandomChance (A)) {
  // option A
else if (RandomChance (B)) {
  // option B
else if (RandomChance (C)) {
  // option C

The chance of:

  • option A = A%
  • option B = B% and not A
    • = B * (100 - A)/100
  • option C = C% and not A or B
    • = C * (100 - A)/100 * (100 - B)/100

This means the chance of B and C is a lot lower than you expect.

If you want the actual probabilities to be 25%, 70%, and 5%, the numbers in your RandomChance should be:

  • 25
  • 70 * 100/(100 - 25) = 93
  • 5 * 100/(100 - 25) * 100/(100 - 93) = 100

(some rounding errors there)

One other alternate method would be using a string of options. Something like:

switch (PickOneString (Split ("A;A;A;A;A;B;B;B;B;B;B;B;B;B;B;B;B;B;B;C"))) {
  case ("A") {
    // do thing A
  case ("B") {
    // do thing B
  case ("C") {
    // do thing C

Log in to post a reply.