Jul 18

True Random Number Generator

Tag: code, matlab, newsadmin @ 6:47 pm

The Quantum Random Bit Generator Service has made available a random number generator that uses quantum theory to create real random numbers:

We use ‘Quantum Random Bit Generator’ (QRBG121), which is a fast non-deterministic random bit (number) generator whose randomness relies on intrinsic randomness of the quantum physical process of photonic emission in semiconductors and subsequent detection by photoelectric effect. In this process photons are detected at random, one by one independently of each other. Timing information of detected photons is used to generate random binary digits – bits. The unique feature of this method is that it uses only one photon detector to produce both zeros and ones which results in a very small bias and high immunity to components variation and aging. Furthermore, detection of individual photons is made by a photomultiplier (PMT). Compared to solid state photon detectors the PMT’s have drastically superior signal to noise performance and much lower probability of appearing of afterpulses which could be a source of unwanted correlations.

They have made a C++ and Matlab toolbox available.


Jun 03

Subtracting time from dates in PHP

Tag: code, phpadmin @ 2:48 pm

We want to subtract 6 months off of a set of dates. Let the date to be modified be defined in the variable $date. To get the timestamp of 6 months prior to this date we run

$unix_timestamp = strtotime("- 6 months", strtotime($date));

Then, to convert the date back into a human-readable format:

$new_date = date('y-m-d', $unix_timestamp);

and $new_date is ‘yyyy-mm-dd.’


Jun 02

Down with 'disp', hooray for 'fprintf'

Tag: code, matlabadmin @ 10:31 pm

Matlab’s disp command display text or variables on the screen and prints it to the diary. Unfortunately, it does not allow one to combine the two. Enter fprintf. Suppose we want to print to the screen the number of remaining observations. With disp this is done as follows:

disp('Observations left:');
disp(num_observations);

disp automatically generates a new line character (\n) after each command, so this looks like:

Observations:
37000

fprintf is both more intuitive and easier on the eyes. To again display the number of observations we write:

fprintf('There are %g observations left in the data.\n', num_observations);

Here, ‘%g’ is replaced with the variable after the comma, “num_observations” and prints:

There are 37000 observations left in the data.

There are many options for the %g that define the conversion. I have found %g to be satisfactory. Check the fprintf page for all the options or type help fprint at the command prompt.


Jun 02

Random integers in Matlab

Tag: code, matlabadmin @ 10:11 am

The rand function in Matlab generates an uniformly distributed random number or set of numbers. If we want to generate a random integer between 1 and n, we type:

floor(rand(1,1)*n) + 1

This creates a random number between 0 and 1 (inclusively) and multiplies it by the maximum integer we are interested in. Then it rounds down so we have an integer and adds one so that the final result is between 1 and n.