|
Post by ivartyn on Jan 27, 2007 14:37:43 GMT
Hi, What code can I use to generate unique number automatically by the program eg. From 1 to 20 +.
Thanks for the help
|
|
buff1
New Member
Posts: 3
|
Post by buff1 on Jan 29, 2007 3:13:11 GMT
If you mean a random number generator it's basically like this RANDOMIZE TIMER D1=INT(RND*20) PRINT D1
|
|
|
Post by Mikrondel on Feb 5, 2007 22:17:15 GMT
There is no way to generate random numbers mathematically. It is however possible to create a sequence of very unpredictable numbers; with the problem that if you do it again, you'll get the same numbers.
QB's RND function does this - it returns a "psedo-random" number between 0 and 1. However, if you run the program over and over, you'll find you end up with the same numbers each time.
RANDOMIZE, however, changes all that. It basically lets you select a starting-point for RND; with a different starting point, you get a different sequence of numbers.
TIMER returns the number of seconds elapsed since midnight (which will be different just about every time you run the program).
Generally you only need to use RANDOMIZE once in your program.
But back to RND - as it returns a number between 0 and 1, to get a different range you need to use this formula:
INT(RND * (High - Low + 1)) + Low
Where High and Low are the lowest and highest integers you want to produce. E.g., to generate integers from -10 to 10:
X = INT(RND * (10 - (-10) + 1)) + (-10)
which can be more simply written:
X = INT(RND * 21) - 10
|
|