Generating Random Numbers
Generating random numbers is a very common task for programmers. But if you aren’t familiar with it, the way they work isn’t very intuitive. This section gives you an overview of what random numbers are and how to use them.
Crystal Reports uses the function Rnd() to return a random number. The purpose of the Rnd() function is to generate numbers that are mostly non-repeating and without a pattern. The numbers are fractional and range between 0 and 1. Some examples are 0.39, 0.77, 0.5, etc. (of course these change each time).
Random numbers are used within a formula by multiplying them against the maximum value in the range of numbers you need. For example, if you need a random number between 0 and 100 then you would multiply the random number by 100. If you need a random number between 0 and 500 then you would multiply it by the number 500. To make things a little more interesting, if you need a number between 25 and 100, then you would multiply it by 75 and add 25. The following code lists some examples for you to use as a reference.
Table 6-10. Sample formulas for generating random numbers.
Desired Values | Formula |
---|---|
Integer Between 0 and 1000 | Formula = Int(Rnd()*1000) |
Integer Between 0 and 25 | Formula = Int(Rnd()*25) |
Fractional number between 50 and 75 | Formula = Rnd()*25 + 50 |
The computer generates random numbers by using a complex algorithm and usually uses the system clock as a starting point. This is called the “seed value’. The problem with random numbers is that since they are calculated using an algorithm, then the numbers aren’t truly random. If the algorithm is passed the same seed value each time, then the algorithm generates the same sequence of numbers. That is why it’s a good idea to use the system clock as the seed value because the time is always changing.
There are times when you don’t want to generate completely random numbers. If you are testing the calculations of a report that uses random numbers, it can be helpful to have the same set of random numbers generated each time so that you can compare the results after each iteration. You can reproduce the same results each time for predictability purposes. Crystal Reports also lets you pass your own seed value to repeat the same random number sequence each time. Again, this helps when creating and testing your report output.
The Rnd() function takes a single argument which tells Crystal Reports what seed value to use when calculating random numbers. The possible argument values are shown in Table 6-11.
Table 6-11. Argument values for the Rnd() function.
Argument Value | Purpose |
---|---|
Any negative number (-1, -500, etc.) | Use this negative number as the seed value for producing the list of random numbers. By using the same negative number it will produce the same list of random numbers. |
Any number greater than zero | Generate the next random number using the system clock as the seed value. |
0 | Return the same random number as the last time the Rnd() function was called. |
By passing different arguments to the Rnd() function you control how random numbers are created and whether the system clock is used as the seed value or not.