Friday, July 20, 2012

What is randomness?


This post is rather a philosophical opinion than anything else. It's going to be a little bit provocative, so if you don't agree with anything, feel free to comment and share your thoughts about it.

Randomness doesn't exist in a strict way. Randomness is only a way to model problems. There's only cause and effect. Let's look at some examples:

When we cast dice, we say the result is going to be random. Using the classic rule of probability, we would say that we have a probability of 1/6 (16.6667%) of getting a specific result when casting a single die. We've model the problem using probabilities and measuring the uncertainty of getting a specific result.

But remember, that's only a model, a tool that represents the problem and gives us solutions (in this case the solutions are in form of probabilities). But what is actually happening isn't random. Let's simplify the problem.

If I put the die with the 3 face up, and only raise it half a millimetre from the floor, when I release the die I will get a 3 for sure!

That's cheating – you may think. And if we're playing risk or backgammon... you're right, I would be cheating. It's cheating because the experiment of casting a die here is quite controlled. I want to get a 3 and I've set the conditions of the experiment to favour my intention.

Well, the experiment isn't quite different if I cast the die at 1 metre from the floor. The only difference here is that I no longer control the experiment and it seems random. So, probabilities here are a good model for the problem, but they doesn't exist for real. Let's assume that I have a supercomputer that is able to take all the variables into account. They could be: initial height, die and floor surface irregularities, floor elasticity, die elasticity, Coriolis effect, wind speed and direction, rotation given by the hand (taking into account that it might be slightly shaking)... Actually I don't even know all the variables of the problem, and I don't think there exists such a computer, but let's assume. If the computer existed and the variables were known the problem can be solved and I would predict the result of the die perfectly.

The same happens when generating a pseudorandom number in a computer. It uses a deterministic algorithm. It might use a “random” seed, but if we know the seed, we know the result. Sometimes the seed it's taking by the time of the clock in milliseconds, but again if we know exactly when this happens, we predict the result.

When we shuffle a deck of cards its the same. If we were able to keep an eye while shuffling, we would know the order of cards in the deck perfectly.

However, we don't know all the variables of all these problems. That's why there seems to be a random entity. So, the probability of an event X occurring, it's just a measure of our ignorance. The higher the chances, the more certain we are that X is going to happen. When we add knowledge to the system, probability changes. This happens for example in the Monty Hall problem. Here you can see a scene of the film 21 in which the problem is discussed:



Final thought. I think that science is about acquiring knowledge of the universe. It's about identifying the cause that produces the observed effect. In the Renaissance, a physician would observe a group of sick people. He would see that all of them had drunk from the same water. Finally, he would conclude that that water is different from normal water. Doing some research he would find a virus in the water. He would then think: “if I kill the virus, health will return”. And using cause an effect he would pull science forward while healing his patients.

Nowadays, so called scientists often say: “the universe exists because of chance”, and what is worse, they get satisfied with their own answer. Whenever we speak of chance there's still knowledge to be unlocked. If you talk in terms of chances, it means that you've model the problem, but you don't fully understand the nature of if. And being satisfied with the answer means that you're comfortable being ignorant about something which definitely is not a trait of the western scientific tradition.

SUMMARY: I'm not saying that randomness is completely useless. In fact, I use it constantly! I love chance games and I love to calculate probabilities and get the best strategy. Randomness is useful because it provides us a way to manage the lack of knowledge. I accept not knowing the result of the die while playing risk, but I don't accept not knowing more important things about the universe and simplify everything with the sentence: “it's all chance”.

Monday, July 9, 2012

My first program using the Tkinter module


I'm starting to learn how to program with Tkinter (a module for Python). I've done some Graphical User Interface (GUI) programming before. I programmed some games using Pygame in the past. Sadly, those games were lost with all the information in my hard drive a few months ago... I would also need a little bit of time to restore my knowledge in the area...

Nevertheless, I wanted to try something different. I started a Tkinter tutorial, and as soon as I was able to do something I wanted to share it. I've programmed a virus.

Well, it's not exactly a virus. It behaves like a virus, but it doesn't compromise the system's security, it is more like a joke. Maybe this is the right time to say that destroying things doesn't make you a hacker, the same way a bomber isn't an architect and a guy who destroys cars isn't an engineer. This program is intended just for fun. Be responsible.

Being said that, let's see how it works.


As you can see in the video, whenever you close one of the windows, another window pops up and the previous one doesn't close. You can try pressing the red X, Alt+F4, etc. It won't work. In order to close the program, press space (it's a little backdoor, if you will).

Download the source code here (it's in a zip file).

If you're curious about what's the image I've used (the monkey), it's “Suzanne” a Blender model used for tests.

Monday, July 2, 2012

Risk simulation: conquering a territory


In previous posts I've showed the probabilities of succeeding in a single attack. In today's post, we'll see how difficult is to conquer an enemy territory. Just as a reminder, you can download the game instructions here. For this simulation, you can download my Python script here.

The simulation will tell you the chances of conquering a territory given the total amount of attackers and defenders. The defenders will be the number of armies in the enemy territory. The attackers will be a number of armies the attacking player wish to risk. So, if you have 7 armies in a territory that is going to attack an enemy territory but you want to make sure that you leave always 4 armies in your territory, you would set the attackers argument to 3 (i.e. 7 – 4 = 3).

During the simulation, I will assume that the attacker will attack with the maximum amount of armies he's willing to risk. The defender will act in the same way (we've concluded in previous posts that it's better to attack using as many dice as possible).

As I did in a previous post, here the simulation will be repeated a number of times to return more accurate data.

Let's prove our intuition. The probability of succeeding in a single attack with one dice against one defender is 41.6667%. So, if I run the program with one attacker and one defender I should conquer the territory a 41.6667% percent of the times or so.

07-02-2012_1


As you can see, the conquest rate is 41.682% (very close to 41.6667%) in a population of one million simulations. However, we already knew that (the result was predicted). Let's try more complex scenarios:

07-02-2012_2


As one should expect, the more armies defending a territory, the harder is going to be the conquest. I've been puzzled for a minute with the results on the losses. In the second to last simulation on the image above, the probability of conquering the territory is around 29.813%, but the defender expects more losses than the attacker. After thinking about it, I find it logical, because even thought the probability of conquest is below 50%, it is taken into account more cases in which the defender losses more armies (i.e. in case of conquest the defender will lose more armies and even if the attacker doesn't conquer the territory, there is also a case in which the defender loses 6 armies, more than the attacker).

We can also conclude that more armies involved in the attempt of conquest will be translated in more casualties (in both sides).

Finally, we already know that attacking with 3 dices will be a likely victory (whether the defenders uses 1 or 2 dices). In the past, I've experienced situations in which two players put many armies in only one territory menacing the other player but never attacking and expending many reinforcements in the business... They were wrong. Be the first one to attack. Let's see it with big numbers:


07-02-2012_3



It took my computer a while to finish this one! Conclusion: as we wanted to prove, even if the enemy outnumbers (not by a huge amount of armies) you, it's better to start the attack.