Board index » delphi » Looking for the algorithm which ramdom uses!

Looking for the algorithm which ramdom uses!

Quote
> I'm wondering if somebody could point me the right direction to finding
> the algoritm that is used in the random function in Borland Pascal 7.0

This depends on what type of random number you want. The byte-type
random word differently from integer and longint types.

Andras

 

Re:Looking for the algorithm which ramdom uses!


On 8 May 1998 02:09:07 GMT, "elab" <electron...@meters.com.au> wrote:

Quote
>    I'm wondering if somebody could point me the right direction to finding
>the algoritm that is used in the random function in Borland Pascal 7.0
>     Thanks in advance,
>    - Julius.

This is the core procedure of the random generator which is called by
Random and Random(w:word). It may be implemented in Pascal litterally
as below.

        const RandSeed:longint=0;

        procedure NewSeed;
        begin
          RandSeed := RandSeed * $8088405 + 1;
        end;

    function Random(w:word):word;   { up to TP 6.0 - BAD ALGORITHM }
    begin
      NewSeed;
      Random  := word(RandSeed SHR 16) MOD w
    end;

a faster Pascal implementation would be

    function Random(w:word):word;   { up to TP 6.0 - BAD ALGORITHM }
    var rr:record lo,hi:word end absolute Randseed;
    begin
      NewSeed;
      Random := rr.hi MOD w
    end;

Pseudo code for Random(w:word):word ( TP 7.0 }

    function Random(w:word):word;   { TP 7.0  - CORRECT ALGORITHM }
    begin
      NewSeed;
{ Pseudo code: Random will return bits 32..47 of the 48 bit result
               of multiplying RandSeed (32 bit unsigned )
               and W ( 16 bit unsigned )

Quote
}

     (* Random := (RandSeed * word) { 48 bit result }  shr 32 *)
    end;

A fast assembler version (much faster than the implemented version) of
Random(w:word) for 386+ would be

    function Random(w:word):word;assembler;
    asm
      db $66 ; mov ax, $8405 ; dw $0808    { mov eax, $08088405 }
      db $66 ; mul word ptr Randseed       { mul RandSeed }
      db $66 ; inc ax                      { inc eax }
      db $66 ; mov word ptr RandSeed, ax   { mov RandSeed, eax }
      db $66 ; xor bx, bx                  { xor ebx, ebx }
               mov bx, w
      db $66 ; mul bx                      { mul ebx }
               xchg dx, ax                 { return dx }
    end;

It uses the 7.0 algorithm and won't be compatible to the output of
TP6.0.

Pseudo code for Random:real/extended, all versions.

        function Random:real/extended;
        begin
          NewSeed;
{ Pseudo code : The nuber 2^32 can't be written as a Pascal
                integral constant
                RandSeed is interpreted as 32 bit unsigned
                in the Range 0..2^32-^1

Quote
}

       (* Random := RandSeed / 2^32 *)
      end;

Regards
Horst

 *** Las orillas del Nahuel Huapi ***

Re:Looking for the algorithm which ramdom uses!


JRS:  In article <01bd7a26$5cb120e0$64ba0ccb@resource1> of Fri, 8 May
1998 02:09:07 in comp.lang.pascal.borland, elab

Quote
<electron...@meters.com.au> wrote:
>Hello!
>       I'm wondering if somebody could point me the right direction to finding
>the algoritm that is used in the random function in Borland Pascal 7.0

        var k : byte ;
        begin
        k := random(3) ;
        end.

Using BP7, write the above program; F2; F9; Alt-T D; cursor to k := ;
F4; Alt-V C; repeated F7 will show you every instruction.

AFAIR, it is based on the usual R[n] = (a*R[n-1] + b) mod c .

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web <URL: http://www.merlyn.demon.co.uk/> - TP/BP/&c. FAQqish topics & links.
  Timo's TurboPascal <A HREF="ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip">FAQ</A>.
  <A HREF="http://www.merlyn.demon.co.uk/clpb-faq.txt">Mini-FAQ</A> of c.l.p.b.

Other Threads