Staredit Network > Forums > SC1 UMS Theory and Ideas > Topic: Randomly choosing a number
Randomly choosing a number
Apr 22 2008, 11:36 pm
By: Clokr_  

Apr 22 2008, 11:36 pm Clokr_ Post #1



There are a whole lot of 'phatom' like maps where they randomly choose a player to be the phantom. When there are 1, 2, 4 or 8 players in the game the task is quite simple: we have to randomize 0, 1, 2 or 3 switches and map each posible outcome to a player.
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

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

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)


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_.



?????

Apr 22 2008, 11:55 pm Riney Post #2

Thigh high affectionado

PRetty sure this 'theory' is a fact, why didnt ye post in teh tutorials? ;o



Riney#6948 on Discord.
Riney on Steam (Steam)
@RineyCat on Twitter

-- Updated as of December 2021 --

Apr 23 2008, 12:39 am Clokr_ Post #3



Quote from name:Dark_Marine
PRetty sure this 'theory' is a fact, why didnt ye post in teh tutorials? ;o

Because it's not a tutorial yet. I had these ideas and I'm asking people for their opinion.
I can also add it to the tutorials later, but I want to get some feedback first.



?????

Apr 23 2008, 12:56 am Falkoner Post #4



Enjoy. It's a tutorial with all the general ways of using switch randomization that I know of, probably the most impressive is the Death-Switch Randomization, it only requires 1 death and one switch for any amount of outcomes, so it's like the basic switch method, but it requires much less variables, and it's similar to your third method.

In my opinion, which method you use depends on what you are using it for, let's say I have an RPG where the player could hit a certain point in the map at any time, then maybe I'll use death counts instead, let's say I want 6 different outcomes in a single trigger loop, I'll use the basic switch method, it all depends on what I need in my map.



None.

Apr 23 2008, 1:21 am Clokr_ Post #5



Your tutorial lacks one picture and its hard as hell to read with that bg... Plus most of the methods you exposed will only work if all the 8 players are playing.



?????

Apr 23 2008, 1:59 am Falkoner Post #6



Quote
Plus most of the methods you exposed will only work if all the 8 players are playing.

How so? It doesn't require multiple players at all....

Oh, and I fixed the image, stupid imageshack randomly deletes my pictures...



None.

Apr 23 2008, 7:32 am JaFF Post #7



Quote from Clokr_
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

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.
I'm using this methos for my map, and I am sattisfied with its functionality. It gives a result within 3 seconds at most (about 12*3 attempts). Running this algorithm while the intro is shown to the players is the best option or me.



None.

Apr 23 2008, 12:54 pm Clokr_ Post #8



Yeah, it's probably the best one. Updated first post with the general mean formula for method II:

Quote
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.




?????

Apr 23 2008, 11:15 pm The_z0r Post #9



This makes me think of my code randomizer.



None.

Apr 24 2008, 6:40 pm Wormer Post #10



The third method is not equiprobable at all. It will make some numbers more probable than others like the first one. On the example for the first method this will look like this: you have two events to represent number 1 - 1 and 7, two events to represent number 2 - 2 and 8 and one event for the remaining numbers. This superfluous outcomes are hauniting you whenever you do. I know no method of equiprobable randomization of numers which are not powers of two and suspect there is no such a method in nature. But what you can do is to make these rerandomization events somewhat very unprobable (as unprobable as you like them to be, there is no limits) or make difference in probabilities as little as you like (no limits again).

You could also randomize large numbers using one switch:
COPY FOR EACH n FROM 0 TO N-1
Randomize sRandom.
If sRandom is set then add 2^n deaths.
ENDFOR
I think it is what falkoner was talking about.



Some.

Apr 24 2008, 9:59 pm Clokr_ Post #11



Quote
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.

Method II outcomes are 100% equiprobable.

And yeah what you did is what falkoner was talking about. But it's equivalent to randomizing n different switches, so only works for powers of two. Then you would have to aply one of the methods that I explained on my first post if you had a different number of possibilities.



?????

Apr 24 2008, 11:58 pm Falkoner Post #12



Yeah, Wormer, in that tutorial I posted it shows exactly how to make it :P



None.

Apr 26 2008, 7:16 am Wormer Post #13



Quote
Quote
Quote
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.

Method II outcomes are 100% equiprobable.
Oh, well, I'm sorry, haven't noticed almost. But talking it is almost equiprobable makes an illusion it is better than the first when it is not like that because they are identical. On a large scale there are only two methods of randomizing. There are only two things you can do, particularly you can attribute redundant outcomes to some of your events and get not equal probability or make rerandomization which needs more time (in mean) necessary to randomize. Everything else is just an implementation of these methods, which differs only the way attributing or rerandomization is done.

Quote
I know no method of equiprobable randomization of numers which are not powers of two and suspect there is no such a method in nature.
To clearfy here I am talking about methods of certain randomization of numbers in one trigger circle.

Quote
Yeah, Wormer, in that tutorial I posted it shows exactly how to make it :P
I've guessed :)

Post has been edited 1 time(s), last time on Apr 26 2008, 7:21 am by Wormer.



Some.

Apr 26 2008, 1:18 pm Clokr_ Post #14



Quote from Wormer
Oh, well, I'm sorry, haven't noticed almost. But talking it is almost equiprobable makes an illusion it is better than the first when it is not like that because they are identical. On a large scale there are only two methods of randomizing. There are only two things you can do, particularly you can attribute redundant outcomes to some of your events and get not equal probability or make rerandomization which needs more time (in mean) necessary to randomize. Everything else is just an implementation of these methods, which differs only the way attributing or rerandomization is done.

Your right, you could use 'Method I' and randomize a lot of switches to make it almost equiprobable. But it takes a whole lot more triggers than 'Method III' does to archieve the same effect.
In the examples, I needed 11 triggers to randomize 8 switches; using 'Method I' I'd need 257 triggers.



?????

Apr 26 2008, 1:44 pm Doodle77 Post #15



Quote
Method II:
The biggest problem is that this algorithm might never end. Nothing ensures us that we'll not keep getting 111 forever.
The chances are very low that it would take more than the 16 trigger cycles Method III will take on average.

Post has been edited 1 time(s), last time on Apr 26 2008, 1:49 pm by Doodle77.



None.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[01:53 am]
Ultraviolet -- :lol:
[06:51 pm]
Vrael -- It is, and I could definitely use a company with a commitment to flexibility, quality, and customer satisfaction to provide effective solutions to dampness and humidity in my urban environment.
[06:50 pm]
NudeRaider -- Vrael
Vrael shouted: Idk, I was looking more for a dehumidifer company which maybe stands out as a beacon of relief amidst damp and unpredictable climates of bustling metropolises. Not sure Amazon qualifies
sounds like moisture control is often a pressing concern in your city
[06:50 pm]
Vrael -- Maybe here on the StarEdit Network I could look through the Forums for some Introductions to people who care about the Topics of Dehumidifiers and Carpet Cleaning?
[06:49 pm]
Vrael -- Perhaps even here I on the StarEdit Network I could look for some Introductions.
[06:48 pm]
Vrael -- On this Topic, I could definitely use some Introductions.
[06:48 pm]
Vrael -- Perhaps that utilizes cutting-edge technology and eco-friendly cleaning products?
[06:47 pm]
Vrael -- Do you know anyone with a deep understanding of the unique characteristics of your carpets, ensuring they receive the specialized care they deserve?
[06:45 pm]
NudeRaider -- Vrael
Vrael shouted: I've also recently becoming interested in Carpet Cleaning, but I'd like to find someone with a reputation for unparalleled quality and attention to detail.
beats me, but I'd make sure to pick the epitome of excellence and nothing less.
[06:41 pm]
Vrael -- It seems like I may need Introductions to multiple companies for the Topics that I care deeply about, even as early as Today, 6:03 am.
Please log in to shout.


Members Online: Ultraviolet, jun3hong