Previous Up Next

9.3.1  Producing uniformly distributed random numbers: rand random alea hasard sample

The rand command produces random numbers, chooses random elements from a list, or creates functions that produce random numbers.
random and hasard are synonyms for rand.

To produce random real numbers:


Examples.


To produce random integers:


Example.
Input:

rand(5)

Output (for example):

3

You can then use rand to find a random integer in a specified interval; if you want an random integer between 6 and 10, inclusive, for example, you can enter:
Input:

6 + rand(11-6)

Output (for example):

7


Another way to get a random integer in a specified interval is with the randint command.


Example.
Input:

randint(6,10)

Output (for example):

8


To make a function which produces random numbers:


Example.
Input:

r:=rand(1.0..2.5):;
r()

Output (for example):

1.68151313369


To choose elements without replacement:


Examples.


The sample command will also randomly select items from a list without replacement.

Note that with the sample command, the list comes first and then the integer.


Example.
Input:

sample(["r","r","r","r","v","v","v"],4)

Output (for example):


"v","v","v","r"

Previous Up Next