diff options
| author | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
|---|---|---|
| committer | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
| commit | 37da2899f40661e3e9631e497da8dc59b971cbd0 (patch) | |
| tree | cbc6d4680e347d906f5fa7fca73214418741df72 /appl/lib/random.b | |
| parent | 54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff) | |
20060303a
Diffstat (limited to 'appl/lib/random.b')
| -rw-r--r-- | appl/lib/random.b | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/appl/lib/random.b b/appl/lib/random.b new file mode 100644 index 00000000..16c7c1ef --- /dev/null +++ b/appl/lib/random.b @@ -0,0 +1,50 @@ +implement Random; + +include "sys.m"; +include "draw.m"; +include "keyring.m"; +include "security.m"; + +sys: Sys; + +randfd(which: int): ref sys->FD +{ + file: string; + + sys = load Sys Sys->PATH; + case(which){ + ReallyRandom => + file = "/dev/random"; + NotQuiteRandom => + file = "/dev/notquiterandom"; + } + fd := sys->open(file, sys->OREAD); + if(fd == nil){ + sys->print("can't open /dev/random\n"); + return nil; + } + return fd; +} + +randomint(which: int): int +{ + fd := randfd(which); + if(fd == nil) + return 0; + buf := array[4] of byte; + sys->read(fd, buf, 4); + rand := 0; + for(i := 0; i < 4; i++) + rand = (rand<<8) | int buf[i]; + return rand; +} + +randombuf(which, n: int): array of byte +{ + buf := array[n] of byte; + fd := randfd(which); + if(fd == nil) + return buf; + sys->read(fd, buf, n); + return buf; +} |
