Randomization

From Staredit Network Wiki
Jump to: navigation, search

Games often need to make use of random numbers. Use one of the methods described below if your map requires randomization.

Switch Methods

Switches have two states, CLEAR (0) and SET (1).

Switch randomization is a built-in function of the Set Switch trigger action.

If two different outcomes is not be enough for you, there are ways of generating higher random numbers using switches.

Multi-Switch

This method will allow you to create a random number within the range of zero to some power of 2.

Here is a list of how many switches you should use in this method, based on the number of outcomes you need:

1  Switch:   2 Outcomes
2  Switches: 4 Outcomes
3  Switches: 8 Outcomes
4  Switches: 16 Outcomes
5  Switches: 32 Outcomes
6  Switches: 64 Outcomes
7  Switches: 128 Outcomes
8  Switches: 256 Outcomes
9  Switches: 512 Outcomes
10 Switches: 1024 Outcomes
11 Switches: 2048 Outcomes 
12 Switches: 4096 Outcomes
13 Switches: 8192 Outcomes
14 Switches: 16384 Outcomes
15 Switches: 32768 Outcomes
16 Switches: 65536 Outcomes

After you determine how many switches you will need, make a trigger to randomize that amount of switches. In this example, we need to select one of the 8 players to go first. There are 8 outcomes, so we will be using 3 switches.

StarCraft Trigger [template]
Players:
  • Your choice
Conditions:
  • Always.
Actions:
  • Randomize Switch 1.
  • Randomize Switch 2.
  • Randomize Switch 3.

With three switches (bits) randomized, there are 8 possible outcomes:

  1. 000
  2. 001
  3. 010
  4. 011
  5. 100
  6. 101
  7. 110
  8. 111

Or, in other words:

  1. Switch1 is CLEAR, Switch2 is CLEAR, Switch3 is CLEAR.
  2. Switch1 is SET, Switch2 is CLEAR, Switch3 is CLEAR.
  3. Switch1 is CLEAR, Switch2 is SET, Switch3 is CLEAR.
  4. Switch1 is SET, Switch2 is SET, Switch3 is CLEAR.
  5. Switch1 is CLEAR, Switch2 is CLEAR, Switch3 is SET.
  6. Switch1 is SET, Switch2 is CLEAR, Switch3 is SET.
  7. Switch1 is CLEAR, Switch2 is SET, Switch3 is SET.
  8. Switch1 is SET, Switch2 is SET, Switch3 is SET.

Then create a trigger for each possible switch outcome.

For example, one could be:

StarCraft Trigger [template]
Players:
  • Your choice
Conditions:
  • Switch 1 is CLEAR
  • Switch 2 is SET
  • Switch 3 is SET
Actions:
  • Whatever is planned for outcome #7.

If you need a number of outcomes that is not a power of 2, use the next-highest power of 2, and just re-randomize for all the outcomes you aren't using. It will eventually land on a valid outcome.

One Switch

This method will allow you to create a random number within the range of zero to some power of 2.

It is possible to use just one switch to make a random number. This method can be easier to use than the multi-switch method because it stores the outcome in a counter, instead of in a bunch of switches. You can use 'at least' and 'at most' with a death counter.

Start by randomizing the switch.

StarCraft Trigger [template]
Players:
  • Your choice
Conditions:
  • Always.
Actions:
  • Randomize Switch 1.

If the switch is set, add 1 to your counter.

StarCraft Trigger [template]
Players:
  • Your choice
Conditions:
  • Switch 1 is SET.
Actions:
  • Modify death counts for Player: Add 1 for Unit.

Randomize the switch again.

StarCraft Trigger [template]
Players:
  • Your choice
Conditions:
  • Always.
Actions:
  • Randomize Switch 1.

If the switch is set this time, add 2 to your counter.

StarCraft Trigger [template]
Players:
  • Your choice
Conditions:
  • Switch 1 is SET.
Actions:
  • Modify death counts for Player: Add 2 for Unit.

Randomize the switch again.

StarCraft Trigger [template]
Players:
  • Your choice
Conditions:
  • Always.
Actions:
  • Randomize Switch 1.

If the switch is set this time, add 4 to your counter.

StarCraft Trigger [template]
Players:
  • Your choice
Conditions:
  • Switch 1 is SET.
Actions:
  • Modify death counts for Player: Add 4 for Unit.

Now the death counter for 'Unit' will have a random number in the range of 0-7. Use the Deaths condition to detect the number.

If you want to make a random number in a higher range, keep randomizing the switch and adding the next highest power of 2 to the counter. See the list above for how many outcomes you get for the amount of times you randomize the switch.

Other Methods

There are some easier, but less accurate, methods of getting random numbers. View these methods as making 'ever-changing' numbers, rather than actual random numbers.

Cycling Counter

The numbers you get from this method will not be random, but if you use some kind of user input, it will be an unpredictable number. (Example: A human player has to take a unit to a beacon to activate the trigger)

Create a trigger that always adds 1 to a counter.

StarCraft Trigger [template]
Players:
  • Your choice
Conditions:
  • Switch 1 is SET.
Actions:
  • Preserve trigger.
  • Modify death counts for Player: Add 1 for Unit.

Also make a trigger that resets the counter to 0 when it goes past a certain range. Our range is 0-7, so 8 is out of bounds.

StarCraft Trigger [template]
Players:
  • Your choice
Conditions:
  • Player has suffered at least 8 deaths of unit.
Actions:
  • Preserve trigger.
  • Modify death counts for Player: Set to 0 for Unit.

Use the Deaths condition to detect the number. Make a trigger to detect each possible outcome. The number you get will depend completely on when the trigger is activated. Use hyper triggers for this method.

Roaming Unit

Units using the junkyard dog AI script move around in an unpredictable manner.

To use this to get a number, first make an area for the unit to move around in, and put locations that cover all parts of it.

Roamingunit.png

Use the Bring condition to detect which location the unit is touching. Make different outcomes based on the location.

Critters are not recommended for this because their movements are not random (they move up and left over 90% more often than they move down or right). Also, units using the junkyard dog AI do not move randomly when they are near the edge of the map (they have an extremely high chance of moving away from the edge).

It is possible for the unit to remain in the same location for a long time.

The quality of this method is questionable. Its usefulness is limited.

Example Map

Here is an example map that shows randomization being used between three different options to play music, you can change the triggers and death counter to adjust timing and what you want the randomized triggers to execute. Map Link