gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gm2] Problem with urandom


From: Martin Kalbfuß
Subject: [Gm2] Problem with urandom
Date: Wed, 21 Oct 2009 23:02:02 +0200

Hi,

I created the following module to produce random integers. But when I
call RandInt I always get the same value. Maybe I do something wrong
about The stream. Opening the file with fopen in C an read a value from
it produces a random number. Do you see anything wrong here? I tried it
with stream file, too. 

Thanks

IMPLEMENTATION MODULE Dice;

IMPORT SeqFile,
       WholeIO,
       IOChan,
       EXCEPTIONS,
       SYSTEM;

VAR urandom : SeqFile.ChanId; 
    res     : SeqFile.OpenResults;
    source  : EXCEPTIONS.ExceptionSource;

PROCEDURE RandInt() : INTEGER;
VAR result : INTEGER;
BEGIN
        WholeIO.ReadInt(urandom, result);
        RETURN result;
END RandInt;

BEGIN
        EXCEPTIONS.AllocateSource(source);

        SeqFile.OpenRead(urandom, '/dev/urandom', SeqFile.raw, res);
        IF urandom = IOChan.InvalidChan() THEN
                                EXCEPTIONS.RAISE(source, 0, 'Error: Could not 
access /dev/urandom');
        END;
FINALLY
        SeqFile.Close(urandom);
END Dice.

The calling module:
MODULE Kniffel;

IMPORT Dice,
       SWholeIO,
       STextIO;
BEGIN
        SWholeIO.WriteInt(Dice.RandInt(), 0);
        STextIO.WriteLn;        
        SWholeIO.WriteInt(Dice.RandInt(), 0);
        STextIO.WriteLn;
        SWholeIO.WriteInt(Dice.RandInt(), 0);
        STextIO.WriteLn;                
END Kniffel.

The results:

+134653772
+134653772
+134653772

Always the same number. All the time. No matter what I do.






reply via email to

[Prev in Thread] Current Thread [Next in Thread]