Monday, October 1, 2001

Random number generation using LotusScript

PROGRAMMING POWER

By Tony Patton

DominoPower has received countless emails requesting more information on LotusScript. This month we begin to address these issues. A frequent solicitation involves random number generation, and it is the focus of this column.

Random number generation

A good question to ask about this subject is what does random mean? A dictionary definition of random is: "having no specific pattern, purpose, or objective."

However, programming is a bit different. Most developers have encountered situations where a random number generator is needed, so we need to know what qualifies as random.

A true random number generator has three important properties:

  • It is unbiased: All values, regardless of sample size, are equiprobable;
  • It is unpredictable: It is impossible to predict the next output, given all previous outputs;
  • It is unreproducible: Two of the same generators, given the same starting conditions, will produce different outputs.

Random number generation in LotusScript

Now let's turn our attention to random number generation within the confines of LotusScript.

LotusScript offers two functions for random number generation: Rnd and Randomize. We'll consider Rnd first.

Rnd

Rnd is an abreviation for random. It generates a real number greater than zero and less than one. Here is a simple mathematic representation of its behavior.

        0 < random_number_generated < 1

The syntax is very simple:

        Rnd()

The parentheses are optional if no arguments are passed. The first example, below, puts the Rnd function to use; a for loop is used to generate ten random numbers. A message box displays the results.

Dim out As String
Dim cr As String
Dim x As Integer
cr = Chr(13)
For x = 0 To 9
 out = out + Cstr(Rnd) + cr
Next x
Msgbox out

Print Rnd

The resulting output is shown in Figure A.

FIGURE A

Here is the output from the first example. (click for larger image)

The return type of the Rnd function is Single. Single is a four byte floating-point value. That is, it's a real number. The result can be multiplied and rounded to receive an integer value. The next example alters the previous code to produce a number between zero and nine.

Dim out As String
Dim cr As String
Dim x As Integer
1.Dim temp As Single
Dim move_decimal As Single
Dim round_value As Integer
2.cr = Chr(13)
3.For x = 0 To 9
4. temp = Rnd
5. move_decimal = (temp * 10)
6. round_value = Round(move_decimal, 0)
7. out = out + Cstr(round_value) + cr
Next x
8.Msgbox out

I've broken the explanation for this code down line by line.

  1. Variables are declared to store various values.
  2. The carriage return character is stored in a String variable. It is used to separate the produced random numbers for display.
  3. The loop executes ten times.
  4. A random number between zero and one is generated.
  5. The decimal place of the random number is moved one place to the right.
  6. The altered value is rounded to produce an integer value. The LotusScript Round function accepts two parameters: number to be rounded and the desired number of decimal places.
  7. The number is placed in a string variable for display.
  8. The results are displayed in a message box.