INFO: How Visual Basic Generates Pseudo-Random Numbers for the RND Function (231847)
The information in this article applies to:
- Microsoft Visual Basic Standard Edition for Windows 1.0
- Microsoft Visual Basic Standard Edition for Windows 2.0
- Microsoft Visual Basic Standard Edition for Windows 3.0
- Microsoft Visual Basic Professional Edition for Windows 2.0
- Microsoft Visual Basic Professional Edition for Windows 3.0
- Microsoft Visual Basic Standard Edition, 32-bit, for Windows 4.0
- Microsoft Visual Basic Professional Edition, 16-bit, for Windows 4.0
- Microsoft Visual Basic Professional Edition, 32-bit, for Windows 4.0
- Microsoft Visual Basic Enterprise Edition, 16-bit, for Windows 4.0
- Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows 4.0
- Microsoft Visual Basic Learning Edition for Windows 5.0
- Microsoft Visual Basic Learning Edition for Windows 6.0
- Microsoft Visual Basic Professional Edition for Windows 5.0
- Microsoft Visual Basic Professional Edition for Windows 6.0
- Microsoft Visual Basic Enterprise Edition for Windows 5.0
- Microsoft Visual Basic Enterprise Edition for Windows 6.0
This article was previously published under Q231847 SUMMARY
The RND function in Visual Basic generates pseudo-random numbers according to a specific algorithm. For certain scientific or statistical studies it might be important to understand how these numbers are generated. This article documents the algorithm used.
A full treatise on the statistical nature of this algorithm is beyond the scope of this article but the topic is widely discussed in the scientific literature.
MORE INFORMATION
Microsoft Visual Basic uses the linear-congruential method for pseudo-random number generation in the RND function. The following pseudo code documents the algorithm used:
x1 = ( x0 * a + c ) MOD (2^24)
where:
x1 = new value
x0 = previous value (an initial value of 327680 is used by Visual Basic)
a = 1140671485
c = 12820163
The 'MOD' operator in the formula above returns the integer remainder after an integer division.
The expression x1/(2^24) will then return the floating-point number
between 0.0 and 1.0 that is returned by the RND function.
Note that the above algorithm cannot be implemented in Visual Basic code in such a way that the random number sequence generated by the RND function can be reproduced. This is because internally Visual Basic uses an unsigned long data type that is not supported by the Visual Basic language.
The following C/C++ code can be used to generate the first ten pseudo-random numbers that Visual Basic generates:
#include "stdafx.h"
int main(int argc, char* argv[])
{
unsigned long rndVal;
rndVal = 0x50000L;
int i;
float rndFloat;
for (i=0;i<10;i++)
{
rndVal = (rndVal * 0x43fd43fdL + 0xc39ec3L) & 0xffffffL;
rndFloat = (float)rndVal / (float)16777216.0;
printf("Value is %.15f\n",rndFloat);
}
return 0;
}
Note that, by default, the Rnd() function will return the same sequence of pseudo-random numbers each time the program is run. For some purposes (such as statistical studies where repeatability is required) this may be appropriate. For other types of applications, such as games, this may not be appropriate. If a different sequence is required, use the Randomize statement prior to the first call to Rnd(). This will initialize the random number seed by using the system timer. If a different sequence is required but must be repeatable in future, use the syntax Randomize X where X is some specific numeric value.
It is important to recognize that Rnd() returns a new sequence for each component in which it is used; that is, if your main EXE generates one sequence and uses a Visual Basic ActiveX DLL to generate a sequence also, these sequences are independent of one another.
REFERENCES
For additional information about how earlier versions of Microsoft Basic generate pseudo-random numbers, please click the article number below
to view the article in the Microsoft Knowledge Base:
28150 RND and RANDOMIZE Alternatives for Generating Random Numbers
Various numerical algorithms for generating pseudo-random number sequences can be found on the Internet and in published texts concerning numerical algorithms.
Modification Type: | Major | Last Reviewed: | 6/24/2004 |
---|
Keywords: | kbinfo KB231847 |
---|
|