Difference between revisions of "Dev random"

From Free Pascal wiki
Jump to navigationJump to search
Line 17: Line 17:
 
   
 
   
 
  '''begin'''
 
  '''begin'''
   i := 0;
+
   i       := 0;
   filemode:=0;
+
   filemode := 0;
 
   AssignFile(f, '/dev/urandom');  
 
   AssignFile(f, '/dev/urandom');  
   reset(f,1);
+
   reset (f,1);
 
   read (f,i);
 
   read (f,i);
 
   CloseFile (f);
 
   CloseFile (f);
 
   RandSeed := i;
 
   RandSeed := i;
 
  '''end''';
 
  '''end''';
 +
 +
= Explanation =
 +
Like everything else in *nix like OS, everything is a file ! so we access urandom as a file. and we read only one byte at a time.
 +
 +
Then we place the data we made as RandSeed (the seed number that the function random uses). And thats it, we have a much random seeder for “random�? usage.

Revision as of 23:10, 21 July 2005

Summery

/dev/random and /dev/urandom are two Unix and *nix based devices that allow receives memory, disc, network dumps from the kernel. This allow us to use “random�? like seed for preforming “random' tasks. with the “random�? function.

The difference between /dev/random and /dev/urandom is the “blocking�? and “non blocking�? issues.

While /dev/random will give us random seeds, it will wait until it will have something to give us, before return with the seed.

/dev/urandom does not stop us from when we do not have new seed from dump, and will return also clock cycles, that are less random, but that way we will not need to wait until any type of activity will occure.

Coding

In order to make Free Pascal work with /dev/random and /dev/random we shell 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 OS, everything is a file ! so we access urandom as a file. and we read only one byte at a time.

Then we place the data we made as RandSeed (the seed number that the function random uses). And thats it, we have a much random seeder for “random�? usage.