Printable VersionPrint
Twitter as a random number generator

Do you think Twitter is noisy? Then try using it to generate random numbers.

Why?

A colleague and I were discussing this tweet ("How many innocent blogs have died so that Twitter may live?" @jkottke) and the conversation led to entropy vs information. I stated that Twitter was full of information, and yet full of entropy. Since random numbers need high-quality sources of entropy, I had the idea to harness Twitter as an entropy source.

How?

By hooking into the RSS feed of Twitter's public timeline, I can get a never-ending source of entropy. I decided to use perl to build a prototype application. This simple script uses some CPAN modules to do the heavy lifting. Since Twitter is used internationally, and in languages other than English, I get a better distribution of characters than a typical password phrase. This means that I can use a single tweet and still generate a decently-random number from it.

The code itself is straightforward: grab the most recent item title from the RSS feed, and convert it to an MD5 hash. Since this is just a prototype, I just display the hex codes for the MD5 hash. In normal usage, I could convert it to a single byte of an integer by selecting the appropriate number of hex "nibbles".

The code

#!/usr/bin/perl

use XML::RSS::Parser::Lite;
use LWP::Simple;
use Digest::MD5 qw(md5_hex);

my $xml = get("http://twitter.com/statuses/public_timeline.rss");
my $rp = new XML::RSS::Parser::Lite;
$rp->parse($xml);

my $item = $rp->get(1);
my $title = $item->get('title');
print md5_hex($title), "\n";

Conclusion

In repeated trials, I found that the RSS feed was cached somewhere, and I was getting the same response several times in a row. To combat this, you may want to use this method to simply seed a regular pseudo-random number generator. As an alternative, if you need a small number of random numbers, simply convert each item title to a random number. Caution: don't abuse the RSS feed by fetching it too rapidly, or it may go away. Article Manager module by by George! Software.