1. Skip to content

1. Random

Useful tips about the random function in Extempore.

1.1 Background

Apparently Standard Scheme does not provide a random number generator. But no worries, there are multiple ways to achieve that as discussed in this SO thread.

Luckily Extempore comes with its own implementation of random. However, the original Extempore version always returns an integer when arguments are provided.

That's not exactly what I wanted as in some situation I'd like to get a random float from any two numbers. So I made a version that does that:

(define (random . args)
  (cond ((< (length args) 1)
         (random-real))
        ((list? (car args))
         (list-ref (car args) (random (length (car args)))))
        ((pair? (car args))
         (apply weighted-selection args))
        (else (let ((lower (if (> (length args) 1) (real->integer (car args)) 0))
                    (upper (if (> (length args) 1) (cadr args) (car args))))
                ;; if first arg is a float => return a float 
                (if (not (integer? (car args))) 
                    (+ (random-real) lower (random-int (- upper lower)))
                    (+ lower (random-int (- upper lower))))
                    ))))

I've updated it on lines 11-12, so that if the first argument it's a float, then it returns a float.

1.2 Random float

(random) ; => 0.689383

Generates a float from 0 to 1.

1.3 Random from list

(random (list 1 2 3 4)) ; => 2

Returns a random element from the list.

Heads up: symbols are evaluated before being returned!

(let ((a 2) (b 3))
  (random (list a b))
    )
; => 2

1.4 Random with boundaries

(random 2 5) ; => 2

Returns a random integer from 2 to 5 (excluded).

(random 2.0 5) ; => 4.867424

Returns a random floating number from 2 to 5 (excluded).

Defaults

(random 2) ; => 0 or 1

The second element is always 0, hence this returns a random integer from 0 to 2 (excluded).

(random 2.0) ; => 0.100261

The second element is always 0, hence this returns a random float from 0 to 2 (excluded).

1.5 Example