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 – Slot Machines

    • 0 Comments

    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.

    image

    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.

     

    image

       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.

  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    Web Development or Computer Science

    • 12 Comments

    I love to look at the logs to see how people find this blog. Some of the search strings people use amaze me. Sometimes I’m not sure which to wonder more about – how did that string find my blog or what in the world are they really looking for. Recently I saw a search for “is it better to study computer science or web development” that was a bit of a puzzler. I’m not sure those are separate subjects. Can you do good web development without a grounding in computer science? Can you be a knowledgeable computer scientist without some knowledge of web development? Probably not.

    I suspect that at the heart of this question was a vocational idea. What career should they go into? Programming (which is what many people think of when they think of computer science) or web development (which many people don’t seem to be aware requires some computer science to do well).

    These days most important web pages have database access, active response, and other “programming” that take them a whole level beyond the static web pages of the past. It’s hard to see how one can keep up with the trends towards AJAX, HTML5, Silverlight, Cloud computing and so much more of the direction of the web without a good computer science background. Sure there are self taught people who do great things  but having a good solid base of computer science knowledge would seem to be a big edge for learning the new technologies.

    Related to this is that I keep seeing people list HTML as a programming language. That confuses me. I’ve worked with mark up languages for years (anyone else remember Runoff?) and while they are useful tools I see them as data rather than as programming languages. Are they part of computer science? For sure. They take in data, meta data, process descriptions and much more. But HTML is not a programming language. Perhaps this is part of that whole confusion that has people thinking computer science and programming are the same thing? Are people calling HTML a programming language an attempt by people to try to justify calling web development computer science? If so, I think they are missing the point. Web development is a part of computer science.

    It may not have the prestige in academic circles that other parts of CS have but it is growing in importance in the “real world” all the time. I think we really want the people developing the next big web app (think Facebook, Twitter and Amazon.com) to know a bit about “the rest” of computer science. What do you think? Is HTML programming? Is web development computer science? Or is it rightly kept separate?

  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    Monte Carlo Simulation - Roulette

    • 2 Comments

    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?

Page 1 of 7 (20 items) 12345»