Archives

Generating random user_ids

December 28, 2007 10:10 am Published by 1 Comment

At work, each new user is assigned a totally random alphanumeric 12-character ID. They’re random instead of sequential because this is what goes into the user’s cookie (and in some cases into URLs) and we didn’t want the IDs to be discoverable. Sometimes we need to do what we call a subscriber load and generate thousands (or sometimes many thousands) of IDs at once. The subload process tends to be very slow, and one of my co-workers was tasked with making it faster. While profiling the code, he discovered that a big time sink was the ID generation procedure. After more research, we discovered that it was written in 2004 and had never been modified after the original checkin. It was hundreds of lines long, used all kinds of global variables (Perl hasn’t had static variables until 5.10), and involved big math with a magic prime number close to 7012. Worse, it was implemented as a hash function. And it was always passed a salt. And that salt was always random.

We replaced it with this code:

my @chars = ('A' .. 'Z', 'a' .. 'z', '0' .. '9');
sub randid {
    $rv = '';
    for my $i (1 .. 12) {
        $rv .= $chars[rand(@chars)];
    }
    return $rv;
}

It used to generate about 100 IDs per second. Now it can do 175,000 per second.

Redirect referer test

November 8, 2007 7:50 am Published by Leave your thoughts

A web user is looking at page A. He clicks on a link for page B. That page has a META Refresh to page C. What is the value of HTTP_REFERER for that last request? What if the redirect was a Status 307? Or a location.replace() JavaScript call? What if he’s using Opera? I’ve been doing some redirect referer tests this week and I have results for some the most common browser/OS combinations. I hope to expand them further.

Sports Uberchampion trivia question

November 4, 2007 12:46 pm Published by Leave your thoughts

Here’s a trivia question (to which I currently do not know the answer) inspired by the Red Sox’s recent World Series win and what I can only hope is soon to be the unbeaten 9-0 Patriots. If you take the 4 major American team professional sports championships (Stanley Cup, World Series, Superbowl, NBA Finals[1]), what’s the longest streak of consecutive championships in one metropolitan area?[2]

[1] – Is it just me, or does basketball not have a very recognizable term for its championship?
[2] – I’m willing to be relatively flexible with the definition of “metropolitan area”.

Update: Answers below the fold!
(more…)

Live photocasting Halloween

October 28, 2007 8:01 am Published by 5 Comments

Sometime during the planning of our two parallel Halloween parties, Chris and I realized we needed some way to allow the two groups to communicate. Videocasting was our first thought, but we didn’t have the equipment or the knowledge. But when it came to taking photos and putting them on websites, we had all kinds of both. Using something Chris had written a while ago as a guide, I wrote a quick script to pull the photos off the camera, resize them to a reasonable resolution, and upload them to our web server. I then wrote a CGI that would pick a random photo from each location and place them side-by-side. Chris asked me to make the algorithm weight towards newer photos, which was far easier than it would have been if we had been uploading to a service like Flickr or something.

After a rough (and late) start in Boston, things went well. Philly took some naughty shots early, which got people riled up here and for a period of time, things were pretty lewd. Eventually it became family-safe fun time party photos and some gentle photo-jabs were traded between the sister parties. Sometime around midnight, Chris texted me “this is the best thing we’ve ever done.” I agree. That said, we learned things. The script had all kinds of bugs (mostly because I wrote it without having the camera we were going to use or the software). The CGI was too weighted towards new photos. And whereas Chris had done this before and had a neat photobooth setup (a side room, tripod, IR trigger), Boston had a camera that had to be hand-shot and plugged back into the laptop every few minutes.

Will we ever be able to learn from the mistakes we made, and try out a new iteration of the script? I certainly hope so. Maybe we can hook in a third city. California friends, I’m gesturing in your direction.

Solitaire probabilities

October 10, 2007 9:12 am Published by 1 Comment

For some reason, I’ve been thinking about Klondike solitaire probabilities a lot lately. Primarily, I’m wondering what the likelihood is that a game will have zero legal plays. I’m certain it happens, but it’s got to be pretty rare. It’s a complex game, though, so here’s my plan towards solving it:

  1. Given two non-ace cards, what is the chance that one cannot be placed on the other, using standard Klondike rules?
  2. Given three non-ace cards, what is the chance that none can be played on any other?
  3. Given seven…?
  4. Given two (three, seven) cards, what is the chance that there is no legal play (placing one on another or moving an ace to the foundation)?

There are more steps after that involving the eight deck cards, but it gets pretty complicated pretty quickly. I’ll be happy just getting this far.