TXR tip of the day.

 new new list compose Reply to this message Top page
Attachments:
+ (text/plain)
+ (text/html)

Delete this message
Author: Kaz Kylheku
Date:  
To: TXR Users
Subject: TXR tip of the day.

Suppose you want to get some bytes out of /dev/urandom, and turn it into an integer. For instance, for use as a seed in a call to make-random-state.

(defun get-urand-num (noctets)
  (with-stream (s (open-file "/dev/urandom"))
    (reduce-left (op + (* @1 256) @2) (take noctets (gun (get-byte s))))))

You pass in how many bytes to read, and get back an integer (arbitrarily large, of course).

Let's break it down: