Staredit Newtork
Community
StarCraft
Games
Site
Favourites
 
[*]
Randomization
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:

Code

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.
Quote
Actions:
- Randomize Switch 1.
- Randomize Switch 2.
- Randomize Switch 3.

With three switches (bits) randomized, there are 8 possible outcomes:
Quote
000
001
010
011
100
101
110
111

Or, in other words:
Quote
Switch1 is CLEAR, Switch2 is CLEAR, Switch3 is CLEAR.
Switch1 is SET, Switch2 is CLEAR, Switch3 is CLEAR.
Switch1 is CLEAR, Switch2 is SET, Switch3 is CLEAR.
Switch1 is SET, Switch2 is SET, Switch3 is CLEAR.
Switch1 is CLEAR, Switch2 is CLEAR, Switch3 is SET.
Switch1 is SET, Switch2 is CLEAR, Switch3 is SET.
Switch1 is CLEAR, Switch2 is SET, Switch3 is SET.
Switch1 is SET, Switch2 is SET, Switch3 is SET.

Create a trigger for each possible switch outcome.
For out example, we will need 8 triggers:
Quote
Conditions:
- Switch1 is CLEAR.
- Switch2 is CLEAR.
- Switch3 is CLEAR.
Actions:
- (Player 1 goes first)

Quote
Conditions:
Switch1 is SET.
Switch2 is CLEAR.
Switch3 is CLEAR.
Actions:
- (Player 2 goes first)

Quote
Conditions:
Switch1 is CLEAR.
Switch2 is SET.
Switch3 is CLEAR.
Actions:
- (Player 3 goes first)

Quote
Conditions:
Switch1 is SET.
Switch2 is SET.
Switch3 is CLEAR.
Actions:
- (Player 4 goes first)

Quote
Conditions:
Switch1 is CLEAR.
Switch2 is CLEAR.
Switch3 is SET.
Actions:
- (Player 5 goes first)

Quote
Conditions:
Switch1 is SET.
Switch2 is CLEAR.
Switch3 is SET.
Actions:
- (Player 6 goes first)

Quote
Conditions:
Switch1 is CLEAR.
Switch2 is SET.
Switch3 is SET.
Actions:
- (Player 7 goes first)

Quote
Conditions:
Switch1 is SET.
Switch2 is SET.
Switch3 is SET.
Actions:
- (Player 8 goes first)

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.
Quote
Conditions:
- Always.
Actions:
- Randomize Switch 1.

If the switch is set, add 1 to your counter.
Quote
Conditions:
- Switch 1 is SET.
Actions:
Modify death counts for Player: Add 1 for Unit.

Randomize the switch again.
Quote
Conditions:
- Always.
Actions:
Randomize Switch 1.

If the switch is set this time, add 2 to your counter.
Quote
Conditions:
- Switch 1 is SET.
Actions:
Modify death counts for Player: Add 2 for Unit.

Randomize the switch **again**.
Quote
Conditions:
- Always.
Actions:
Randomize Switch 1.

If the switch is set this time, add 4 to your counter.
Quote
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.
Quote
Conditions:
Always.
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.
Quote
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
Critters (or 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.
(user posted image)

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

It is possible for the unit to remain in the same location for long time.
The quality of this method is questionable. Its usefulness is limited.


[09:24 pm]
madroc -- tazz that is a great avvy
[09:23 pm]
madroc -- haha falkoner
[09:22 pm]
GameLoader1337 -- if you guys are temple siege fans perhaps can you look at what i posted on the thread tell me what you think
[09:21 pm]
GameLoader1337 -- hey guys
[09:21 pm]
Falkoner -- I modeled the SeN skin on my site off of Carbonite/SeN :P
[09:20 pm]
madroc -- wow I didn't see notice the margins that is niice
[09:17 pm]
Falkoner -- Even if I have a 1280x1024 monitor, I still like a smaller size, it's annoying reading across a massive screen
You must log in to shout.

©2003-2008 Staredit Network.
Starcraft & Starcraft II are trademarks of Blizzard Entertainment.
Site Index   |   Terms of Service   |   Privacy Policy   |   Contributions