Sunday, July 27, 2014

How Simulations Saved Me From Vegas

A few weeks ago, I came up with the same seemingly brilliant but foolish idea that a lot of hopeful gamblers have thought of. I had just returned from a mildly successful weekend Vegas trip, and I had gambling on my mind. In particular, I was thinking about games of pure luck. Games like Roulette or Casino War.

My first trip to a casino as an 18 year old was unsuccessful. My roommates and I had been practicing counting cards for weeks, and decided to try our luck. Turned out, it was a lot easier to remember card counts and strategy sitting in our living room. I've always enjoyed that small adrenaline rush that comes with making a bet, but it doesn't really help you when you're trying to do math in your head. We shifted our focus to poker, but that's a blog post for another time.

I wasn't planning on making money on my most recent trip. I was there for a friend's birthday. I didn't want to spend 8 hours sweating at a blackjack table. Also, I lost $100 on Blackjack during the first few hours I was there, so I was kind of done with that game. Instead, I wanted to play games of pure luck. It turned out to be strangely relaxing. When I got back, I started doing some math. Questions like "What's the expected value of a Casino War bet?" started popping up in my head. I've got a bad habit of trying to figure stuff out before I google it.

Then, I had an epiphany. What if I double my bet every time I lose? Here's what I came up with.
  • Go to a table with $1270 ($10 + $20 + $40 + $80 + $160 + $320 + $640)
  • If I win a bet, put that $10 aside.
  • If I lose a bet, double it for the next round.
  • Stop when my original $1270 is gone.
  • Otherwise... profit.
I would have to lose 7 bets in a row to lose all my money. In roulette, if I bet on red, I've got about a 53% chance of losing. On any given betting round, that's about a 99% chance I win $10. It was foolproof. I wrote up some code to simulate it, and to see if I had finally beaten the casinos. Surprisingly, my simulations lost money. A lot of it.

I ran it by someone at work, who quickly pointed out that it was still a horrible bet, and that I wasn't the first to think of it. It's called a Martingale Betting System, and it doesn't work. Well, it does if you have an infinite amount of money, which is a problem for me.

First off, that 1% chance that I lose is still a big hit over the long run, because I'm losing $1270. That 99% chance that I win $10 doesn't cover me enough to give me a positive expected value.

Still, I had some code that made some fancy charts, and I wanted to show it off.
The results of the simulations were still kinda cool. I made 3 gamblers. They would all win and lose at the same times.
  • A flat better who always bet the same amount
  • A martingale better with no betting restrictions
  • A martingale better with a maximum bet
Here were some of the results:



After 15 bets, the martingale better was looking pretty good. I hadn't come anywhere close to the $500 limit, so the 2 martingale betters were doing the same. So, lets see what happens after 100 bets.


Still not too bad. There's a few spots where the martingale betters have to make some pretty big bets, and it takes a bit for the limited one to catch back up, but they still did way better than the flat better. Let's do this with 10000 bets...



The unlimited martingale better did great! But the more realistic limited martingale better did crazy bad. He can't make up for his losses fast enough. He just looses money faster than the flat better.

So... What did I learn? Casinos are smart. Maximum bets exist for a reason. Next time, I'm going to do some googling before I get too excited about something.

I posted the code I used to make the graphs on Github here. Feel free to take a look. Don't judge me too harshly, I usually don't do too much in Python.

Friday, July 25, 2014

How I Won the Company Costume Contest: Using CURL to Submit HTTP Forms

The Problem

It was Halloween, and there was a costume contest at the office. I had a beard, and I love Bill Murray, so I threw on an orange ski hat, cheap aviators, and a blue shirt. I was Steve Zissou. It was pretty glorious but my coworkers failed to recognize me. Everyone thought I was supposed to be the uni-bomber. I was not amused.

The people who dressed in costumes all got together for pictures. Someone made up a quick Google survey and sent out a link to vote on your favorite costume. After I made my decision, I noticed I could submit more than one vote. Classic mistake.

The survey was a simple form. All I needed to do was submit a bunch of these forms, with my name selected.



The Solution

After a bit of googleing, I came across a program called Curl. It’s a bit like wget if you’re familiar. Curl is a client that can send to (or receive files from) a server .The command is designed to work without user interaction or any kind of interactivity.

Curl can do a lot of stuff, but all I needed to do was make some HTTP posts.

I opened up the developer tools in chrome (F12), clicked the network tab, started logging, and submitted the form.
This gave me all the information I needed.
I opened up an editor, and wrote something along the lines of this:
curl -v --cookie "COOKIE_COPIED_FROM_CHROME" --data "FORM_DATA_COPIED_FROM_CHROME"
https://docs.google.com/url/copied/from/chrome
I saved that script in a file called “vote”. I wanted to repeat my form submission a lot, so I ran something like this in a linux shell:
for i in {1...1337}; do ./vote; sleep 1; done
i.e. Vote for me every second for 1337 times.

The Results

Needless to say, I dominated the competition. Unfortunately, our company has less than 200 employees, so my landslide victory was a bit suspicious. If I had known there was going to be a $100 prize, I might have been a bit more subtle.

All in all, Curl was a fast, free, easy to use scripting tool. I don't think I've ever had that much entertainment from a ten minute script.