Dev random

This is an old revision of Dev random, edited by BigChimp at . The current revision is 30625.

Categories: applies to FPC (rather than Lazarus); has example code, so adding it to tutorial makes sense

Summary

/dev/random and /dev/urandom are two Unix and *nix based devices that process memory, disc, network dumps from the kernel. This allows us to use a "random" like seed for performing "random" tasks with the "random" function.

The difference between /dev/random and /dev/urandom is whether the device is "blocking" or "non blocking":

Coding

In order to make Free Pascal work with /dev/random and /dev/random we can write the following code:

procedure RandomSeed;
var
  f : file of integer;
  i : integer;

begin
  i        := 0;
  filemode := 0;
  AssignFile(f, '/dev/urandom'); 
  reset (f,1);
  read (f,i);
  CloseFile (f);
  RandSeed := i;
end;

Explanation

Like everything else in *nix like OSes, everything is a file! So, we access urandom as a file and we read only one integer at a time.

Then, we place the data we made as RandSeed (the seed number that the function random uses). And that's it, we have a random seeder for "random" usage.