# Random Number Functions

On this page I present a set of functions involving the generation and manipulation of pseudo-random numbers. Information about the behaviour, parameters and returns of each function is provided below, in addition to several examples of use.

## Pseudo-Random Number Generator

 Function Syntax (LM:rand) Current Version 1.0
Arguments
None
Returns
Type Description
Real A pseudo-random number between 0 and 1

### Function Description

This pseudo-random number generator (PRNG) function utilises a Linear Congruential Generator to return a pseudo-random number between 0 and 1. The multiplier, modulus and increment parameters of the linear congruential generator are derived from the book Numerical Recipes by William H. Press, Saul A. Teukolsky, William T. Vetterling and Brian P. Flannery.

The initial 'seed' for the linear congruential generator is the value of the DATE System Variable at the time at which the function is evaluated. The seed value is subsequently bound to the symbol \$xn which remains global.

Select all
```;; Rand  -  Lee Mac
;; PRNG implementing a linear congruential generator with
;; parameters derived from the book 'Numerical Recipes'

(defun LM:rand ( / a c m )
(setq m   4294967296.0
a   1664525.0
c   1013904223.0
\$xn (rem (+ c (* a (cond (\$xn) ((getvar 'date))))) m)
)
(/ \$xn m)
)
```

### Example Function Calls

```_\$ (LM:rand)
0.147379
_\$ (LM:rand)
0.817889
```

## Random in Range

 Function Syntax (LM:randrange ) Current Version 1.1
Arguments
Symbol Type Description
a Integer The lower bound of the range
b Integer The upper bound of the range
Returns
Type Description
Integer A pseudo-random integer in the given range (inclusive)

### Function Description

This function returns a pseudo-random integral number in the inclusive range specified by the supplied integer arguments; i.e. if the lower bound of the range is 1 and the upper bound is 4, the function will randomly return either 1, 2, 3 or 4.

Select all
```;; Random in Range  -  Lee Mac
;; Returns a pseudo-random integral number in a given range (inclusive)

(defun LM:randrange ( a b )
(+ (min a b) (fix (* (LM:rand) (1+ (abs (- a b))))))
)
```

### Example Function Calls

```;; Generate a pseudo-random number between 1 & 6 (inclusive)
_\$ (LM:randrange 1 6)
2
_\$ (LM:randrange 1 6)
1
```

textsize

# Jump To:

Designed & Created by Lee Mac © 2010