gm2
[Top][All Lists]
Advanced

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

Re: [Gm2] Problem with urandom


From: Martin Kalbfuß
Subject: Re: [Gm2] Problem with urandom
Date: Sat, 12 Dec 2009 04:32:34 +0100

Thanks for this,

I'm currently working on the SDL wrapper, so this isn't of interest at
the moment. But I will keep it in mind for my later work.


Am Freitag, den 11.12.2009, 12:11 +0000 schrieb Gaius Mulley:
> Martin Kalbfuß <address@hidden> writes:
> 
> > 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.
> 
> Hi Martin,
> 
> 
> I've been wanting to get to the heart of this problem for sometime
> as:
>    od -x /dev/random
> 
> does yield a seemingly random sequence of hex bytes.  I think there is
> a bug in the code:
> 
> 
> > PROCEDURE RandInt() : INTEGER;
> > VAR result : INTEGER;
> > BEGIN
> >     WholeIO.ReadInt(urandom, result);
> >     RETURN result;
> > END RandInt;
> 
> 
> should be:
> 
> 
> PROCEDURE RandInt() : INTEGER;
> VAR
>    result : INTEGER;
>    n      : CARDINAL;
> BEGIN
>    (* WholeIO.ReadInt(urandom, result); *)
>    IOChan.RawRead(urandom, SYSTEM.ADR(result), SIZE(result), n) ;
>    IF n#SIZE(result)
>    THEN
>       EXCEPTIONS.RAISE(source, 0, 'Error: Could not read SIZE(INTEGER)
>       number of bytes from /dev/urandom');
>    END ;
>    RETURN result;
> END RandInt;
> 
> trying to read the raw bytes as a string is likely to hit a problem.
> On my machine it gave me zero each time as the first character was
> not in the range '-+012345679'.  However with the modifications above
> I now get:
> 
> -725512388
> +517208660
> +900127671
> address@hidden:~/GM2/ThirdPartyTests/non-free/martin-kalbfuß$ ./a.out 
> +2121694642
> +1452980218
> -1248358007
> address@hidden:~/GM2/ThirdPartyTests/non-free/martin-kalbfuß$ ./a.out 
> -531434612
> -1484797461
> +1139867131
> address@hidden:~/GM2/ThirdPartyTests/non-free/martin-kalbfuß$ ./a.out 
> -403106276
> -129135769
> +1581116526
> address@hidden:~/GM2/ThirdPartyTests/non-free/martin-kalbfuß$ ./a.out 
> -865335780
> -1686289691
> -526479807
> address@hidden:~/GM2/ThirdPartyTests/non-free/martin-kalbfuß$ ./a.out 
>  +82033948
> -861875516
> -215214017
> address@hidden:~/GM2/ThirdPartyTests/non-free/martin-kalbfuß$ ./a.out 
> -622441135
> -853786266
> +472446955
> 
> 
> regards,
> Gaius


-- 
Martin Kalbfuß <address@hidden>





reply via email to

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