|
|
|
|
|
|
|
|
|
|
|
|
|
|
PROGRAMMING POWER
Random number generation using LotusScript
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:
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. Roll over picture for a 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Advertisement --
Learn Notes and Domino 8 at your place and pace!
Learn Notes and Domino in your office and/or home! TLCC's highly acclaimed distance learning courses for users, developers, and admins will enhance your career and your resume.
The many included activities and demos will make you a pro! Expert instructor help is a click away.
Click here to try a FREE demo course!! |
-- Advertisement --
Want The Top Lotus Experts By Your Side Without Paying Hefty Consulting Fees? Look No Further.
Like having a team of consultants by your side -- ones who have all the answers and never make mistakes -- THE VIEW gives you immediate access to field-tested instruction, guidance, and best practices from the brightest Lotus professionals around.
Join your peers who realize their Lotus technology is too important to let people from blogs and forums tell them how they should implement it, run it, and use it. THE VIEW is where only the world's top Lotus experts provide validated support to you on a weekly basis to ensure you work more efficiently, get more out of your Lotus technology, and stay clear of costly mistakes.
Check out the new instruction, tips, and best practices added to THE VIEW this week. |
|
|
|
|
|
|
|
|
|
|