OOo Basic generates floating-point random numbers ranging from 0 through 1. Random numbers generated by computers aren't, in general, random. An algorithm is used that generates "random numbers" based on a previous random number. The first number on which all other random numbers are based is called the "seed." You use the Randomize function to specify the seed value. If a value is omitted, the Randomize function uses a value obtained from the system timer. Specifying the starting seed allows you to test programs and generate the same sequence of random numbers each time.
The Rnd function is used to generate the random numbers. Table 9 shows the documented methods to call the Rnd function, even though OOo Basic ignores all arguments.
Function | Description |
---|---|
none | If no argument is included in the call, a standard sequence of random numbers is generated. |
> 0 | A positive argument has the same effect as no argument. Other than checking that the number is positive, the argument is effectively ignored. |
= 0 | The most recently generated random number is returned. |
< 0 | If the argument is negative, the argument is used to generate the next random number. Each time the same negative number is used, the same random number is returned. |
Bug | Table 9 reports the behavior claimed in the OOo help as of version 1.1.1. After reading the source code, however, I can say that the argument is ignored and the next random number in the sequence is returned, regardless of the value of the argument. |
Randomize(4) 'The next two numbers should be the same each time Print Rnd() 'Some number from 0 through 1 Print Rnd() 'Another number from 0 through 1
The random number generated is some number from 0 through 1. To obtain a different range, perform a few mathematical operations. For example, multiplying a number between 0 and 1 by 10 yields a number between 0 and 10. To use a range that does not start at 0, add an appropriate offset. See Listing 21 .
![]() |
Function RndRange(lowerBound As Double, upperBound As Double) As Double Dim RangeWidth As Double RndRange = lowerBound + Rnd() * (upperBound - lowerBound) End Function
![]() |
Use an appropriate function, such as CInt or CLng, if you want a whole number rather than a floating-point number.
CLng(lowerBound + Rnd() * (upperBound - lowerBound))
I had two functions that solved the same problem-determining the GCD (Greatest Common Divisor) of two integers-and I wanted to know which was faster. I generated random integers and called each routine a few thousand times. While performing timing tests, it's important to use the same data for each trial. I was able to use random numbers because the Randomize statement allows me to generate the same random numbers every time.
Randomize(2) 'reset the random number generator to a known state t1 = getSystemTicks() For i = 0 To 30000 n1 = CLng(10000 * Rnd()) n2 = CLng(10000 * Rnd()) call gcd1(n1, n2) Next total_time_1 = getSystemTicks() - t1 Randomize(2) 'reset the random number generator to a known state t1 = getSystemTicks() For i = 0 To 30000 n1 = CLng(10000 * Rnd()) n2 = CLng(10000 * Rnd()) call gcd2(n1, n2) Next total_time_2 = getSystemTicks() - t1