La generación de números pseudo-aleatorios es una característica que la mayoría de lenguajes de programación implementa por defecto, pero Pascal estándar extendido es una excepción (el FreePascal no, ojo).

Esta historia viene porque hace tiempo necesitaba generar estos números para un programa en Pascal, y al no encontrar nada (aunque bueno... tampoco busqué mucho :P ) programé  algo. No es gran cosa, si se conocen los dos números anteriores y el momento en el que se inició el generador (con una precisión de microsegundos) se puede intentar descubrir cual será el siguiente, pero sirve para algo básico.

Para usarlo solo hay que importarlo y llamar a la función random usando el número "tope" como parámetro, por ejemplo, para generar un número del 0 al 100 (ambos incluidos):

=============================================================================== aleatorio := random(100); ===============================================================================

El código es este, se basa en el cifrado ARC4 (a.k.a RC4), y para descargar [random.pas]

=============================================================================== module mod_random;

{ Copyright (C) Kenkeiras 2010

This program is free software: you can redistribute it and/or modify     it under the terms of the GNU General Public License as published by

the Free Software Foundation, either version 3 of the License, or     (at your option) any later version.

This program is distributed in the hope that it will be useful,     but WITHOUT ANY WARRANTY; without even the implied warranty of     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the     GNU General Public License for more details.

You should have received a copy of the GNU General Public License     along with this program. If not, see http://www.gnu.org/licenses/.

Modulo de numeros aleatorios

Contiene las funciones para generar numeros aleatorios

- random

}

export mod_random=(random);

function random(top:integer):integer; type     tbyte=0..255;

var     S: array [0..255] of tbyte;     i,j: integer;

end;

{ procedure swap (var c1:tbyte;var c2:tbyte);     Intercambia dos bytes } procedure swap (var a,b:tbyte);

var      temp:tbyte;     begin      temp:=b;      b:=a;      a:=temp;     end;

{ procedure random_seed;

Inicia la caja para generar los numeros pseudo-aleatorios

} procedure random_seed;     var         t:timeStamp;         str:string(255);         n:integer;

begin         getTimeStamp(t);         str:=time(t);

for n:=0 to 255 do             S[n]:=n;

j:=0;         for n:=0 to 255 do begin             j:=(j+S[n]+ord(str[(n mod length(str))+1]))mod 256;             swap(S[n],S[j]);         end;

i:=0;         j:=0; end;

{ function random_byte:tbyte;

Precondicion: La caja esta iniciada     Devuelve un byte pseudo-aleatorio

} function random_byte:tbyte;     begin         i:=(i+1)mod 256;         j:=(j+S[i])mod 256;

swap(S[i],S[j]);         random_byte:= S[(S[i]+S[j])mod 256];     end;

{ function random(top:integer):integer;

Precondicion: La caja esta iniciada     Devuelve un numero (integer) pseudo-aleatorio con un valor maximo de 'top'

}

function random;     var         n,k:integer value 0;

begin         for n:=((top div 256)+1) downto 0 do             k:=k+random_byte;

random:=(k mod top)+1;     end;

to begin do     { Al iniciar el programa inicia la caja para poder generar numeros aleatorios }     random_seed;

end.

Y eso es todo... hasta otra.