Random Thing Spawner, Part Deux: Weighted Randomness

"Weighted randomness? What the hell?" is probably what you're thinking right now. Don't deny it - I called the psychic hotline and they said you were! Weighted randomness is just my term for it, but basically it's just theoretical probability. For our health dispenser, let's make a soul sphere spawn 10% of the time, medikit spawn 40% of the time, and stimpack 50% of the time. We do this again with the lovely modulus operator. But first, the basics.


script 81
  {
    int i;

    i = rnd();

What we've done is assigned i to the random number. This ensures that while the checking is being done that i remains constant. First, we'll give the 10% probability to the supercharge. Remember that rnd chooses a random number from 1 to 100 (inclusive). Therefore, 10% of the numbers are divisibile by 10. So 10% of the time, i % 10 will be equal to 0.


i = rnd();
if(i % 10 == 0)
  {
    spawn(SUPERCHARGE, 128, 0);
    return();
  }

To break that down: first, it finds whether the random number divided by 10 is 0. If so, it moves into the braces; otherwise, it skips. In the braces, it spawns a supercharge at (128, 0), then quits the current script (the return function). Now that we've got the soul sphere ready, we'll do the medikit. How do we tell it to have 40% of the time? Well, i % 2 == 0 will be true 50% of the time. However, since any numbers that had i % 10 == 0 are not accounted for at this point, 40% remain.


...
if(i % 2 == 0)
  {
    spawn(MEDIKIT, 128, 0);
    return();
  }

Finally, we need to make a stimpack be spawned the other 50% of the time, which is done by simply telling it to spawn. (If a soul sphere or medikit was spawned, they have exited the script.)


script 81
  {
    ...
    spawn(STIMPACK, 128, 0);
  }
The final products ends up looking something like that. Note that the health spawner's percentages are based on theoretical probability, not practical probability. If you press the health spawning switch 10 times, you won't get a soul sphere, 4 medikits, and 5 stimpacks unless you're _really_ lucky. Hopefully the numbers should be around that, though. So, load up the map and spawn some stuff!