Author |
Topic: problem with PRINT RND (Read 803 times) |
|
Malcolm
Guest
|
 |
Re: problem with PRINT RND
« Reply #3 on: Feb 20th, 2010, 7:56pm » |
|
Actually it was a discussion between Richard and Jon where this came up.
And there is a actually problem with using a variable like pc% it's too small.
SYS "QueryPerformanceCounter", ^T% seed% = RND(-ABS(T%))
Now that works. Note that variable U% is also written to. The returned data is bigger than one integer's worth so it overflows into the next static variable.
The discussion is Message 5168 on the BB4W group.
|
|
Logged
|
|
|
|
Richard Russell
Guest
|
 |
Re: problem with PRINT RND
« Reply #4 on: Feb 20th, 2010, 11:01pm » |
|
on Feb 20th, 2010, 6:33pm, Guest-Jonathan Zerd wrote:| I can't tell the machine how not to generate the same numbers. |
|
If I understand your question correctly, you want to generate a set of 'random' numbers in which you can guarantee the same value won't appear more than once.
Supposing you want to choose six lotto numbers, each from the range 1 to 49, but the same number mustn't be used twice. This is one way to do it:
Code: total = 49
DIM lotto(total)
FOR I = 1 TO total
lotto(I) = I
NEXT I
FOR choice = 1 TO 6
R = RND(total)
PRINT lotto(R)
lotto(R) = total
total = total-1
NEXT choice
Each time around the loop the program chooses only from the numbers that are left, so the same number cannot be chosen twice. This exactly mimics what happens in a real lottery machine.
The issue of not choosing the same number more than once has nothing to do with the degree of 'randomness'.
Richard.
|
|
Logged
|
|
|
|
Richard Russell
Guest
|
 |
Re: problem with PRINT RND
« Reply #5 on: Feb 20th, 2010, 11:07pm » |
|
on Feb 20th, 2010, 7:56pm, Guest-Malcolm wrote:SYS "QueryPerformanceCounter", ^T% seed% = RND(-ABS(T%)) |
|
Please... we have structures now!! There's no reason to use this horrible technique any more. Far better to use:
Code: DIM pc{l%,h%}
SYS "QueryPerformanceCounter", pc{}
seed% = RND(-ABS(pc.l%)) And, yes, I know there is some example code in the BB4W docs which still uses the ^V% technique; I only spotted it recently and it's already been fixed in the online version.
Richard.
|
|
Logged
|
|
|
|
Malcolm
Guest
|
 |
Re: problem with PRINT RND
« Reply #6 on: Feb 21st, 2010, 5:47pm » |
|
All of which says that R% = RND(-TIME) probably is as good as any means.
But your interpretation of the original question is probably right so randomization it isn't needed in this instance.
I am going back and fixing all my shuffle routines.
|
|
Logged
|
|
|
|
|