Tag Archive: Programming

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.

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.

Upgrade and coComment

August 12, 2006 8:46 am Published by Leave your thoughts

Some behind-the-scenes stuff: I’ve finally upgraded this blog from WordPress 1.5 to 2.0.4. While I was at it, I decided to install the coComment plugin, so that you can follow your comments with coComment. They have a new Firefox extension that makes it even easier than before. Almost trivial, in fact.

Update, 13 Aug – I’ve created a small plugin to add the slash:comments element to my RSS feed. That should fix the discussion counts on my home page, and it’s a cleaner solution than hacking the WP code directly.

Four random Saturday links

August 5, 2006 2:56 pm Published by Leave your thoughts

Here’s a random Saturday link-dump:

  1. Don’t believe BusinessWeek’s bubble-math – Web 2.0 plus shoddy journalism equals a firm foundation for another bubble. BusinessWeek takes a made up number, multiplies it by a rumored percentage, contradicts itself several times, and most readers are probably just thinking “Wow, what a smart kid!” Related: A hilarious parody.
  2. Saved locations on Google Maps – This is a great thing. I’ve been waiting for some sort of smart auto-complete on Google Maps since day one. The interface is a little crusty (I wish I could click on a bubble anywhere and say “save this location” instead of having to have all locations saved), but I’m certain this is just release number one.
  3. No Space World and Mario Galaxy could be available at launch. Or rather, no one has yet verified that Mario Galaxy won’t be available at launch. Related: The early October release rumors still seem to have some air in them.
  4. Two Cool Bash Tricks – Holy cow. Both are total life savers, but the second more than the first. Redirecting output to two files before you can diff them is a big pain in the neck. (via)