When there are 3, 5, 6 or 7 players the task is not that simple. I've been thinking about it and found some algorithms which may work.
That problem (when there are n players) is identical to the problem of choosing a random number between 0 and n-1, so I'll address the second problem instead.
Method I - Mapping every possible output
This means that if we have to randomize a number between 0 and 5 we can randomize 3 switches and map every possible outcomes to one of the 6 numbers. For example:
Code
000 -> 0
001 -> 1
010 -> 2
011 -> 3
100 -> 4
101 -> 5
110 -> 0
111 -> 1
001 -> 1
010 -> 2
011 -> 3
100 -> 4
101 -> 5
110 -> 0
111 -> 1
The biggest problem of this method is that if n is not a power of 2 we cannot have equiprobability.
Method II - Throwing away wrong results
When we get a not valid outcome we rerandomize the switches and check them again. This way we ensure equiprobability. E.g:
Code
000 -> 0
001 -> 1
010 -> 2
011 -> 3
100 -> 4
101 -> 5
110 -> Randomize again
111 -> Randomize again
001 -> 1
010 -> 2
011 -> 3
100 -> 4
101 -> 5
110 -> Randomize again
111 -> Randomize again
The biggest problem is that this algorithm might never end. Nothing ensures us that we'll not keep getting 111 forever. This is not likely to happen anyway and even in the worst case (randomizing a number between 0 and 2^m so we have to randomize m switches thus the probability of failing an attempt is ~0.5) the mean of attempts needed to get a right outcome is 1/0.5 = 2.
Actually, if we're randomizing m switches and we have n possible succesful outcomes, we'll expect this method to randomly choose a right outcome in ~(2^n)/m attempts.
Method III - Modulo algorithm
The idea behind this method is randomizing a number between 0 and an high 2 power, then using the division modulo operation to transform it into a number between 0 and n. This method ensures almost equiprobability and constant time.
A good SC map implementation would be randomizing a great number of switches, then composing the binary number using on a death counter and finally substracting (n+1) until the death counter value is lower than (n+1).
This example will randomize a number between 0 and 7:
Code
Cond: Always
Actions:
- Randomize Switch 1
- Randomize Switch 2
- Randomize Switch 3
- Randomize Switch 4
- Randomize Switch 5
- Randomize Switch 6
- Randomize Switch 7
- Randomize Switch 8
Cond: Switch 1 is set
Actions:
- Clear switch 1
- Add 1 to death counter
Cond: Switch 2 is set
Actions:
- Clear switch 2
- Add 2 to death counter
Cond: Switch 3 is set
Actions:
- Clear switch 3
- Add 4 to death counter
Cond: Switch 4 is set
Actions:
- Clear switch 4
- Add 8 to death counter
Cond: Switch 5 is set
Actions:
- Clear switch 5
- Add 16 to death counter
Cond: Switch 6 is set
Actions:
- Clear switch 6
- Add 32 to death counter
Cond: Switch 7 is set
Actions:
- Clear switch 7
- Add 64 to death counter
Cond: Switch 8 is set
Actions:
- Clear switch 8
- Add 128 to death counter
Cond: Death count is atleast 8
Actions:
- Substract 8 from death counter
- Preserve trigger
Cond: Death counter is almost 7
Actions:
- (We have a random number in our death counter that ranges from 0 to 7)
Actions:
- Randomize Switch 1
- Randomize Switch 2
- Randomize Switch 3
- Randomize Switch 4
- Randomize Switch 5
- Randomize Switch 6
- Randomize Switch 7
- Randomize Switch 8
Cond: Switch 1 is set
Actions:
- Clear switch 1
- Add 1 to death counter
Cond: Switch 2 is set
Actions:
- Clear switch 2
- Add 2 to death counter
Cond: Switch 3 is set
Actions:
- Clear switch 3
- Add 4 to death counter
Cond: Switch 4 is set
Actions:
- Clear switch 4
- Add 8 to death counter
Cond: Switch 5 is set
Actions:
- Clear switch 5
- Add 16 to death counter
Cond: Switch 6 is set
Actions:
- Clear switch 6
- Add 32 to death counter
Cond: Switch 7 is set
Actions:
- Clear switch 7
- Add 64 to death counter
Cond: Switch 8 is set
Actions:
- Clear switch 8
- Add 128 to death counter
Cond: Death count is atleast 8
Actions:
- Substract 8 from death counter
- Preserve trigger
Cond: Death counter is almost 7
Actions:
- (We have a random number in our death counter that ranges from 0 to 7)
The best method is probably method II because it is not bad, flexible and simple to implement. If anyone knows any other method of choosing random numbers using switches please post it.
Post has been edited 3 time(s), last time on Apr 23 2008, 12:55 pm by Clokr_.
?????