[Oberon] ... MOD 65536

Jörg joerg.straube at iaeth.ch
Thu Apr 9 11:29:03 CEST 2020


Hans

Just take the well-proven random generator.
Below I inserted my version in Oberon-07

br
Jörg
. . . . . . 
MODULE RandomNumbers;

IMPORT Kernel;

(* Park-Miller 1993 *)
CONST a = 48271; m = 7FFFFFFFH; q = m DIV a; r = m MOD a;

VAR z*: INTEGER;

PROCEDURE Uniform*(): REAL; (* returns a random nbr 0 < x < 1 *)
	(*   z = a * z MOD m   by Schrage's method *)
	BEGIN
		z := a*(z MOD q) - r*(z DIV q);
		IF z < 0 THEN INC(z, m) END
	RETURN FLT(z) / FLT(m)
	END Uniform;

PROCEDURE RND*(max: INTEGER): INTEGER;	(* returns an integer in the range 0..max-1 *)
	RETURN FLOOR(Uniform() * FLT(max))
	END RND;

PROCEDURE InitSeed*(seed: INTEGER);
	BEGIN
		IF seed = 0 THEN z := 1 ELSE z := seed END;
	END InitSeed;

BEGIN
	InitSeed(Kernel.Time())
END RandomNumbers.



Am 09.04.20, 10:22 schrieb "Oberon im Auftrag von Hans Klaver" <oberon-bounces at lists.inf.ethz.ch im Auftrag von hklaver at dds.nl>:

    Sorry: 65636 in the text should be 65536.
    
    > 
    >    seed := (seed * 1309 + 13849) MOD 65536  (* use MOD 65636 to get the same output as C *)
    >    RETURN seed                         (* in V5 use MOD 65535 to prevent a crash in Tree *)
    >  END Rand;
    > 
    > In Oberon '... MOD 65536' produces the same pseudorandom numbers as '... & 65535' in C.
    > I can understand that: at the bit level '... MOD 65536' is equivalent to '... & 65535'.
    > 
    > But could anyone explain why '... MOD 65636' gives a crash in Tree and '... MOD 65535' does not?
    
    --
    Oberon at lists.inf.ethz.ch mailing list for ETH Oberon and related systems
    https://lists.inf.ethz.ch/mailman/listinfo/oberon
    




More information about the Oberon mailing list