scheme shell
about
download
support
resources
docu
links
 
scsh.net

Random Integer From Dev

random-integer

Read four bytes from /dev/urandom and construct an integer.

-Eric

 (define *integer-length* 4) ; integer length in bytes

(define (bytes->integer byte-string) (let ((l (map char->ascii (string->list byte-string)))) (let loop ((b l) (r 0) (d 0)) (if (null? b) r (loop (cdr b) (bitwise-ior r (arithmetic-shift (car b) d)) (+ d 8))))))

(define (random-integer) (let ((port (open-input-file "/dev/urandom"))) (cond ((read-string *integer-length* port) => (lambda (r) (close-input-port port) (bytes->integer r))) (else (error "Could not read from /dev/urandom")))))


RandomIntegerFromDev - raw wiki source | code snippets archive