Difference between revisions of "Dev random"

From Free Pascal wiki
Jump to navigationJump to search
(Language, category, syntax highlighting. Apparently using level 2 headers only is good wiki practice...)
Line 1: Line 1:
= Summary =  
+
== Summary ==
/dev/random and /dev/urandom are two Unix and *nix based devices that allow receives memory, disc, network dumps from the kernel. This allows us to use “random�? like seed for performing “random' tasks with the “random�? function.
+
/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 the “blocking�? and “non blocking�? issue.
+
The difference between /dev/random and /dev/urandom is whether the device is "blocking" or "non blocking":
  
While /dev/random gives random seeds, it will wait until it will have something to give us, before returning the seed.  
+
* While /dev/random gives random seeds, it will wait until it has something to give us, before returning the seed.  
  
/dev/urandom does not stop us from when we do not have a new seed from dump, and will return also clock cycles that are less random. This way we will not need to wait until any type of activity will occur.
+
* /dev/urandom does not stop us from when we do not have a new seed from dump, and will return also data that are less random. This way we will not need to wait until any type of activity will occur, but randomness is reduced.
  
= Coding =
+
== Coding ==
In order to make Free Pascal work with /dev/random and /dev/random we shall write the following code:  
+
In order to make Free Pascal work with /dev/random and /dev/random we can write the following code:  
 +
<syntaxhighlight>
 +
procedure RandomSeed;
 +
var
 +
  f : file of integer;
 +
  i : integer;
  
'''procedure''' RandomSeed;
+
begin
'''var'''
 
  f : '''file of''' integer;
 
  i : integer;
 
 
'''begin'''
 
 
   i        := 0;
 
   i        := 0;
 
   filemode := 0;
 
   filemode := 0;
Line 24: Line 24:
 
   CloseFile (f);
 
   CloseFile (f);
 
   RandSeed := i;
 
   RandSeed := i;
'''end''';
+
end;
 +
</syntaxhighlight>
  
= Explanation =  
+
== Explanation ==  
Like everything else in *nix like OS, everything is a file! So, we access urandom as a file and we read only one integer at a time.
+
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.
+
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.
  
 
[[Category:Statistical algorithms]]
 
[[Category:Statistical algorithms]]
 +
[[Category:Unix]]

Revision as of 10:04, 22 April 2013

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":

  • While /dev/random gives random seeds, it will wait until it has something to give us, before returning the seed.
  • /dev/urandom does not stop us from when we do not have a new seed from dump, and will return also data that are less random. This way we will not need to wait until any type of activity will occur, but randomness is reduced.

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.