Random Numbers in Lisp

Here’s the translation of the Go code to Lisp, along with explanations in Markdown format suitable for Hugo:

(defun main ()
  ;; The random:uniform function returns a random float between 0 and 1
  ;; We multiply it by 100 and round down to get an integer between 0 and 99
  (format t "~a,~a~%" 
          (floor (* 100 (random:uniform)))
          (floor (* 100 (random:uniform))))

  ;; random:uniform directly gives us a float between 0 and 1
  (format t "~a~%" (random:uniform))

  ;; To get a float between 5 and 10, we multiply by 5 and add 5
  (format t "~a,~a~%" 
          (+ 5 (* 5 (random:uniform)))
          (+ 5 (* 5 (random:uniform))))

  ;; To create a random number generator with a fixed seed
  ;; we can use make-random-state
  (let ((r2 (make-random-state t)))
    (format t "~a,~a~%" 
            (random 100 r2)
            (random 100 r2)))

  ;; Creating another generator with the same seed
  ;; will produce the same sequence of numbers
  (let ((r3 (make-random-state t)))
    (format t "~a,~a~%" 
            (random 100 r3)
            (random 100 r3))))

(main)

This Lisp code demonstrates the generation of random numbers, equivalent to the original example. Here’s a breakdown of the code:

  1. We use random:uniform to generate random floats between 0 and 1. To get integers between 0 and 99, we multiply by 100 and use floor.

  2. random:uniform directly gives us a float between 0 and 1, similar to rand.Float64().

  3. To generate floats in a specific range (like 5.0 to 10.0), we scale and shift the output of random:uniform.

  4. In Lisp, we can create a random number generator with a fixed seed using make-random-state. This is similar to creating a new rand.Source in Go.

  5. We demonstrate that creating two random states with the same seed produces the same sequence of numbers.

When you run this program, you might see output like this:

68,56
0.8090228139659177
5.840125017402497,6.937056298890035
94,49
94,49

Note that the actual numbers will vary each time you run the program, except for the last two lines which should be identical due to the fixed seed.

In Lisp, the random package provides various functions for generating random numbers. You can refer to your Lisp implementation’s documentation for more details on available random number generation functions.