debian update
[kmrcl.git] / random.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          random.lisp
6 ;;;; Purpose:       Random number functions for KMRCL package
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
11 ;;;;
12 ;;;; KMRCL users are granted the rights to distribute and use this software
13 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
14 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
15 ;;;; *************************************************************************
16
17 (in-package #:kmrcl)
18
19 (defun seed-random-generator ()
20   "Evaluate a random number of items"
21   (let ((randfile (make-pathname
22                    :directory '(:absolute "dev")
23                    :name "urandom")))
24     (setf *random-state* (make-random-state t))
25     (if (probe-file randfile)
26         (with-open-file
27             (rfs randfile :element-type 'unsigned-byte)
28           (let*
29               ;; ((seed (char-code (read-char rfs))))
30               ((seed (read-byte rfs)))
31             ;;(format t "Randomizing!~%")
32             (loop
33                 for item from 1 to seed
34                 do (loop
35                        for it from 0 to (+ (read-byte rfs) 5)
36                        do (random 65536))))))))
37
38
39 (defmacro random-choice (&rest exprs)
40   `(case (random ,(length exprs))
41      ,@(let ((key -1))
42          (mapcar #'(lambda (expr)
43                      `(,(incf key) ,expr))
44                  exprs))))
45