Computer Science Teacher
Computer Science Teacher - Thoughts and Information from Alfred Thompson

December, 2009

  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    Monte Carlo Simulation - Roulette


    In theory, theory and practice as the same. In practice, all too often, they are not. Sometimes running a simulation can help one separate theory and reality. For example, about a month ago I received an email that promised me success at the roulette tables. This is a game I’m not into because it seems too much about luck. But there was this email and it sounded good. Part of it is below.

    you know in roulette you can bet on blacks or reds. If you bet $1 on black and it goes black you win $1 but if it goes red you loose your $1.

    So I found a way you can win everytime:

    bet $1 on black if it goes black you win $1

    now again bet $1 on black, if it goes red bet $3 on black, if it goes red again bet $8 on black, if red again bet $20 on black, red again bet $52 on black (always multiple you previous lost bet around 2.5), if now is black you win $52 so you have $104 and you bet:

    $1 + $3 + $8 + $20 + $52 = $84 So you just won $20 :)

    now when you won you start with $1 on blacks again etc etc. its always bound to go black eventually (it`s 50/50) so that way you eventually always win.

    Not the best written piece of prose but it caught my attention. As far as I can tell it was really a come on to try this scheme at an online casino. Not a chance in the world I’m going to try that. But would this work? I’m sure there is a solid mathematical way to find out but I don’t know what it is. So I decided to run a simulation.

    Low and behold it seemed to work just fine. But then I looked into it some more. It turns out that you don’t ahve a 50/50 chance of winning. There are two locations on the wheel in the US that are neither red or black (0 and 00). So the odds are 1.111 against you not 1 to 1 as the email I received would indicate. Does that make a difference? Turns out it does. You can still win but it is not a sure thing. And in fact at times the amount one has to bet can get very large very quickly. This runs the risk that the better will run into a limit on the amount the casino allows a better to make. In fact in several simulations I ran the program tried to bet in the billions of dollars and crashed.

    I leave creating your own simulation as an exercise for the user. What do you see as a result?

  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    Keeping It Simple


    Sample code is a great way to learn how to do things in computer programming. The problem for many beginners is that a lot of sample code is written by professionals for professionals. This means several things both good and bad. On the good side students are exposed to good coding practices (usually), technically correct code, and powerful techniques. On the bad side sample code for professionals often is very complicated (perhaps even over engineered) and often presents more new concepts than a beginner can safely handle. The problem than becomes separating what a beginner needs for a specific task and what has been included for more complicated problems. This is why I tend to write a lot of sample code and even better highlight sample code by better programmers/explainers than myself. Matt Gertz of the Visual Basic team is one of those people. If you search my blog for his name you’ll find a number of references to his posts on the Visual Basic Team Blog.

    Recently I came across something he posted back in 2007 (Simple Animation in Visual Basic (Matt Gertz)) that explains some simple animation techniques in Visual Basic. He does a great job and I recommend the article. The gerbil code (there is a link to it at the bottom of the article or go here includes two simple projects. The Gerbil game and a very simple project that moves a sheep across the screen.

    Along with the animation, Matt also includes a couple of other things that are well worth knowing. One of them is states as in state machines. I think that while he doesn’t spend a lot of time on the concept of a state machine the implementation is one that students can easily understand and work with. Once they know about that in practice I think you will find explaining the theory goes quickly and easily. The other thing that a student can pick up easily is drag and drop programming. Students like to do that and it is surprisingly easy once you know the steps.

    For what it is worth I created as basic a sample of drag and drop as I could. This code just moves a picture box around the screen in response to pressing, holding and moving the mouse. When you release the  mouse button the picture box stays were you leave it.

       1: Dim offsetX As Integer
       2: Dim offsetY As Integer
       3: Dim isMoving As Boolean
       5: Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
       6:             Handles PictureBox1.MouseDown
       7:     ' Get some initial offset values for the mouse location and flag that we are moving the box
       8:     offsetX = e.X
       9:     offsetY = e.Y
      10:     isMoving = True
      11: End Sub
      13: Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
      14:             Handles PictureBox1.MouseMove
      15:     ' we will move the box if isMoving is true and we will move it in relation to the mouse movement
      16:     If isMoving Then
      17:         PictureBox1.Left += e.X - offsetX
      18:         PictureBox1.Top += e.Y - offsetY
      19:     End If
      20: End Sub
      22: Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
      23:             Handles PictureBox1.MouseUp
      24:     ' We are done moving the box so change the value of isMoving to false
      25:     isMoving = False
      26: End Sub

    Try it. Have some fun with it. This week is all about the fun of programming for me.

  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    Monte Carlo Simulation – Slot Machines


    Random numbers are fun. Really they are. They are at the heart of gambling for sure but they are also a key part of what is called a Monte Carlo Simulation. These simulations are used for all sorts of scientific, financial, engineering and other simulations. They are an attempt to figure the effects of chance, of random reactions, to a formula/algorithm or plan. As such they are an important tool in many fields. But of course Monte Carlo refers to the famous casino so its association with gambling is pretty much unavoidable. This even though the term originated with a group of nuclear researchers that included the computer pioneer John von Neumann. And as it turns out I have a couple of gambling simulations in mind that I think make interesting projects.

    The first was inspired by this slot machine simulator that I found as a result of a Tweet on Twitter.


    It’s pretty fancy and clearly some serious research went into it. Just for fun I decided to create a very simple version of a slot machine simulation. I didn’t do much research so it’s not as scientific but it was fun to do. The form is below and you can see that I used textboxes to allow the user to specify the starting amount, how much to bet on each spin and how many spins to do. The payoff rate I hard coded in but could easily be another variable the user could set. The payoffs is determined by an other random number and could be a lot more fancy. Probably should be.



       1: private void button1_Click(object sender, EventArgs e)
       2:       {
       3:           int bankRoll = Int16.Parse( this.textBox1.Text);
       4:           int bet = Int16.Parse(this.textBox2.Text);
       5:           int cnt = Int16.Parse(this.textBox3.Text);
       6:           Random r = new Random();
       7:           for (int i = 0; i < cnt && bankRoll >= bet; i++)
       8:           {
       9:               if (r.Next(100) > 90)
      10:               {
      11:                   bankRoll += r.Next(100);
      12:               }
      13:               else
      14:                   bankRoll -= bet;
      15:           }
      16:           this.label1.Text = bankRoll.ToString();
      17:       }

    Making this better is an exercise for the student. I hope to write this up in some more detail at a future date but I’m on vacation so it will wait. But there are a couple of things I do like about it. One is that I get to use a more complex terminating clause than we often see in an early student project. It allows for some discussion about the roll of negative numbers in loops. Do you allow them? It depends on the application doesn’t it? Slot machines typically do not grant credit. And of course there are those random numbers to look at so you can get into all sorts of discussions about random numbers, statistics and streaks. Why do you sometimes win? Good stuff.

Page 1 of 7 (20 items) 12345»