Article ID: 28150 - Last Review: August 16, 2005 - Revision: 2.1 RND and RANDOMIZE Alternatives for Generating Random Numbers
This article was previously published under Q28150 On This PageSUMMARY
If you want a substitute for RND and RANDOMIZE, you can use your own
equation to generate random numbers as shown below.
Microsoft Basic offers the RND function to return random single-precision numbers between 0.000000 and 1.000000. The RANDOMIZE statement can be used to reseed (or initially start) a given sequence returned by RND. Microsoft Basic uses the linear-congruential method for random-number generation in the RND function. This information is also included with the Help file provided with the Standard and Professional Editions of Microsoft Visual Basic for MS-DOS, version 1.0. MORE INFORMATION
Microsoft Basic uses the linear-congruential method for random-number
generation in the RND function. The following is an example of the
linear-congruential method formula, similar to that used by RND in
Microsoft Basic:
x1 = ( x0 * a + c ) MOD 2^24
x1=new number
(Note: the MOD operator in the formula above returns the integer
remainder after an integer division.)
x0=previous number a=214013 c=2531011 The expression x1/(2^24) returns a floating-point number between 0.0 and 1.0. Please refer to Code Examples 1 and 2 below for an illustration. For more random number generation algorithms, see pages 353-364 of "Microsoft QuickBASIC Programmer's Toolbox," by John C. Craig, published by Microsoft Press (1988). Seven random number subprograms are documented, and a companion disk in MS-DOS format is also available from Microsoft Press. The programs in Craig's book are written for Microsoft QuickBasic for MS-DOS, version 4.0 for the IBM PC. Some programs, such as the random number programs, are general and can easily be modified to run in Microsoft QuickBasic for the Apple Macintosh. When you run these programs, you may want to reseed the random number sequence regularly (such as every few hundred invocations) for greater uniformity. Code Example 1The following is an example of the linear congruential method for generating pseudo-random numbers:Code Example 2The following is the same as Example 1, except the random numbers are plotted to illustrate their uniform distribution:APPLIES TO
| Other Resources Other Support Sites
CommunityArticle Translations |






Windows Live
Facebook
Twitter
Linkedin
Digg it
Yahoo
Delicious
StumbleUpon
Yammer
Reddit
Technorati
FriendFeed
Email

Back to the top