2009-09-10

Airplane problem simulator

I just got back from a party where a friend of mine told me the classic "Airplane seating problem", and the resulting answer. The answer was somewhat hard for me to believe, so, since I had brought my laptop, I pulled it out and spent 10 minutes writing this program to simulate the airplane problem. It turns out that the answer he had told me is correct. I'll be darned.

Anyway, here's the program, written in Java. Paste it into a file called PlaneSim.java, run "javac PlaneSim.java" and then "java PlaneSim" to run it. The javadoc at the beginning of the program explains what the airplane problem is, in case you haven't heard of it before.

import java.util.Random;

/**
* There's a common puzzle that goes something like this: There's a plane that
* has 100 seats in it. Each person has a ticket that assigns them to a
* particular seat. However, the first person doesn't follow his ticket, instead
* choosing a random seat to sit in. Each successive person tries to sit in the
* seat listed on their ticket. If that seat is already taken, they choose a
* random vacant seat to sit in. This goes on until all 100 passengers have been
* seated. What is the chance that the last person will be in his correct seat?
* The puzzle's correct answer is 0.5 (IE he's in his correct seat about half of
* the time on average). This program simulates the airplane problem several
* thousand times to verify this answer, and it does, indeed, verify that 0.5 is
* the correct answer.
*
* @author Alexander Boyd
*
*/
public class PlaneSim
{
private static int seatcount;

/**
* @param args
*/
public static void main(String[] args)
{
seatcount = 100;
// Run 100,000 simulations
int simcount = 100000;
int correctTimes = 0;
for (int i = 0; i < simcount; i++)
{
correctTimes += ((sim() == (seatcount - 1)) ? 1 : 0);
}
System.out.println("Simulation count: " + simcount);
System.out.println("Last person was in correct seat " + correctTimes
+ " times");
}

public static int sim()
{
int[] seats = new int[seatcount];
// Change all seats to be empty
for (int i = 0; i < seats.length; i++)
{
seats[i] = -1;
}
// Run the simulation
for (int i = 0; i < seatcount; i++)
{
if (i == 0)
{
// First person, so random seat
seats[random()] = 0;
}
else
{
// Put this person in their seat if it's empty
if (seats[i] == -1)
{
seats[i] = i;
}
// If it's not empty, randomly place this person until we get an
// empty seat to put them in.
else
{
int randomIndex = random();
while (seats[randomIndex] != -1)
randomIndex = random();
seats[randomIndex] = i;
}
}
}
// Simulation is done. Now see who's in the last seat.
return seats[seatcount - 1];
}

private static Random random = new Random();

public static int random()
{
return random.nextInt(seatcount);
}
}

2009-09-04

Natural bee repellant

This is a new discovery made by my mom that I figured might be helpful to other people. In our yard, we grow spearmint. I don't remember exactly why she tried using it against bees, but placing a cutting of spearmint on our picnic table has proved to be a very effective bee repellant. Like I mean one day we were getting swarmed with meatbees while eating outside, so we put a cutting of spearmint on the table, and we didn't see a single bee after that. So if you want to get rid of bees, plant spearmint and use cuttings of it whenever you eat outdoors.

And if you're going to plant some spearmint, I should probably tell you that it's extremely agressive in terms of spreading by root. So plant it in a pot for best results.

2009-08-07

A test blog post

I'm posting this with a GNOME applet to see if it works. So everyone can pretty much ignore this post.

UPDATE: it appears to work, except that it doesn't place the title correctly.

2009-07-07

Chinese Firebox

This one is my dad's brainchild. He came up with the design for it a couple of years ago, and we've built one at my grandma's every 4th/24th that we're there since. Here's a video:


Cool, huh. The whole thing is made with the contents of 24 crackle bombs (the little green round things about the size of a smoke bomb). It takes its name from the fact that the actual firework, once built, looks like a miniature take-out box from a Chinese restaurant. I'll post instructions on how to make one, along with some pictures of the actual firework, sometime in the future.

2009-05-26

Compiz and vanished animations in Jaunty

I figured I should probably mention this in case anyone has this problem and is stupid enough not to see the solution like I did...

I recently upgraded my system to Ubuntu Jaunty, and noticed that a ton of window animations in compiz had disappeared. This, of course, frustrated me, as the burn animation is my personal favorite and the one I use on all normal windows.

For some reason (and I can't figure out why), the animation add-ons appear to be disabled by default in Jaunty. Or maybe it happened when it upgraded compiz, I'm not really sure and I don't know enough about Ubuntu to say. But either way, enabling the animation add-ons fixed the problem. So anyone out there, if you're all the sudden missing a ton of animations, make sure that animation add-ons is enabled before you go to irc.freenode.net/#ubuntu and make a royal idiot of yourself.

2009-05-12

TrayTimer

I spent most of today writing another useful utility, TrayTimer. It adds a tray icon that you can click to set timers, much like a kitchen timer. Here's a screenshot:




When you click on the tray icon, that window pops up, much like when you click on the clock in Windows Vista. You can then add timers using the text fields on the right. The timers that you've added show up to the left. You can click on the button that shows the time remaining to pause the timer. When a counting-down timer reaches 0, a window opens up that says "Timer 1: Time's up".

The code for the program is available at http://jwutils.googlecode.com/svn/trunk/projects/text/binutils/src/com/googlecode/jwutils/timer/. Download all four source code files, compile them, and run TrayTimer. Or you can svn export that url and compile it. I'll post a download soon that contains the whole thing already compiled.

2009-05-11

Javawizard's Surveyer now up and running

I just got another web application up and running, Javawizard's Surveyer. It's a web application for creating and taking surveys. It's not fancy or anything (I wrote it in less than 2 days), but it's easy to use, and free. In a few days, I'm going to add the ability to actually embed a survey within an email. When you open an email with a survey, there isn't a link or anything; the email actually appears inside of the survey. I've tested it out with gmail and yahoo mail, and it works with both.

2009-05-03

Pastebin API for Java

For those out there that would like to access pastebin.com from within a java class to create, read, and delete posts, I decided to write a class that does just that. Currently, it only creates pastebin posts, but I'm working to add functionality to read and delete posts as well.

You can download the current version at http://code.google.com/p/jwutils/source/browse/trunk/projects/jzbot/src/org/opengroove/jzbot/utils/Pastebin.java. This will automatically update as I add more code to the class.