Coder’s Corner - 1000 Monkeys

monkey2What happens when you get three computer geeks in a pub? They talk about monkeys.

While a feeble attempt was made to discuss standard small talk items such as politics and sport, the focus inevitably turned to computers. So what do computers and monkeys have in common?

The infinite monkey theorem loosely states that a monkey hitting keys at random on a keyboard for an infinite amount of time will almost surely type or create a particular chosen text, such as the complete works of William Shakespeare. This idea has several variations including a thousand monkeys on a thousand typewriters. Even the Simpsons and Ricky Gervais had a go at it.

This colorful conversation led us to a hot topic in computing today - spam. How do they know where I live in cyberspace anyway? There are a plethora of ways they can find out, including checking sites you’ve visited or sending little programs to seek you out. But there’s another way they can get you - 1000 monkeys. In effect, what if a super computer out there randomly selected a bunch of letters and numbers, and stuck them in front of @hotmail.com, and emailed each one out? Most would bounce back. But you know what – it wouldn’t take very long before it got one right.

I woke up a little worse for wear the next morning, but excited about the conversation we’d had the night before, and eager to put this theory to the test. So I wrote a little program that stuck together 7 random letters and compared them to just 10 usernames from our school. It took 94 million guesses before it got one right. Sounds like a lot, but it only took about half an hour on my piddly little machine, and it was only looking for a handful of high school teachers. A state-of-the-art machine could email the latest Viagra ad to thousands of ungrateful people in a matter of minutes. Scary.

Random Roll

Now for today’s program. It won’t be as complicated as the above example, but it will continue with today’s theme: generating random values. How would you like to be able to generate a random order of students for class presentations at the click of a button? This little program will do the trick.

You’ll have to do a bit of work before you use it, however. You will need to enter your roster in Notepad, one student on each line, like in the picture on the left.

Be sure to only press enter once after you’re finished typing the last person. When you pick up my program save it in a folder in your My Documents. Save the Notepad file there too, and call it “class”. Then you should be good to go. The link for the program is below. Enjoy!

Random Roll 1.0

(right click, then “save link as”)

Inside the Coder’s Studio

The code for this one is actually quite short. I’ll break it down into sections to make it easier to understand.

First of all, I needed to create an array. An array is basically like a table in Word - each box of the table can be accessed by a number - 0 being the first, 1 being the second, etc. I take all the names of your students from the text file and stick them into the array. This allows me to more easily manipulate the names. (remember: the green text are my comments explaing the code, not the code itself)

Then I loop (execute a piece of code over and over) the number of times as there are students. Each time I go through the loop, I do 3 things.

1) I randomly generate a number (between 0 and the number of students I have left). I look in the array box that is associated with that number, and choose that student to put in the list next.

2) I put that student’s name in the label so you can see it.

3) I then get rid of that student’s name from the array (so I won’t choose it again), by shifting all the names after it up one place in the array.

Once the loop has executed all the times it’s supposed to, everyone should be in the label for you to use!

Loop

Like last time, feedback is welcome, and the program is completely customizable. If it’s close to what you want or need, but not quite there, shoot me a comment and I will be happy to fix it to your specifications!

1 Comment so far »

  1.  

    Darren said

    November 6 2007 @ 2:32 pm

    An interesting Website for building Web based applications, is this the future of programming?
    http://www.coghead.com/

Comment RSS · TrackBack URI

Leave a comment

Name: (Required)

eMail: (Required)

Website:

Comment: