Sun, 20 Feb 2005

Truly random numbers

The neat random.org service started by Mads Haahr a few years ago supplies truly random numbers. This is not the place to get into details about why pseudo and quasi (aka low-discrepancy sequences) random numbers are only 'random'.

Mads' service samples atmospheric noise -- see his background essay for more details -- which gets aggregated and can then be had via Corba, HTTP or SOAP. Given how R has such a wonderful (and probably little know) url() function to aquire data over the web, I figured it might be worthwhile to show how R can acquire truly random numbers. This snipped downloads a 10,000 x 2 vector, and plots it:

> X <- read.table(url(paste("http://www.random.org/cgi-bin/randnum",
                            "?num=10000&min=-1000000000&max=1000000000&col=2",
                            sep="")),
                      header=FALSE)
> plot(X, pch=".")
(The paste() is used to split the overly long line for the full URL.) The arguments to the interface at random.org are hopefully self-explanatory. Otherwise, full details are available.

As repeated simulations are often rather time-intensive, downloading random sequences may not be the fasted way to go about things. However, this method would provide a portable way to seed a pseudo random number generator in a portable fashion for platforms that do not have an entropy provider under /dev.

/computers/R | permanent link