BBC BASIC for Windows
Programming >> Graphics and Games >> problem with PRINT RND http://bb4w.conforums.com/index.cgi?board=graphics&action=display&num=1266694415 problem with PRINT RND
Post by Jonathan Zerd on Feb 20th, 2010, 6:33pm
Hello, i am a newbie at programming and i need some help with the function PRINT RND. The problem is that while i am trying to create a KENO-like game, i can't tell the machine how not to generate the same numbers.Here is the code; INPUT "how many numbers? " Op% IF Op%=1 THEN INPUT "guess a number: " Num1 PRINT RND(8) PRINT RND(8) PRINT RND(8) ... ... Thank you in advance, Jo
Re: problem with PRINT RND
Post by MALCOLM on Feb 20th, 2010, 7:10pm
I think you are saying that you keep getting the same number sequence.
You can avoid this by giving the RND function a seed. Here is one that was suggested some time ago.
REM Randomize the RND function. (Otherwise it is predictable.) SYS "QueryPerformanceCounter", ^pc% seed% = RND(-ABS(pc%))
seed% is not used after this it, is just somewhere to catch the value.
The performance counter will give a "randomish" seed to the pseudo random RND. That should be enough randomization. Re: problem with PRINT RND
Post by David Williams on Feb 20th, 2010, 7:45pm
I use R% = RND(-TIME) to seed BB4W's pseudo-random number generator.
It's nearly always sufficient for my purposes, but possibly not as 'good' as the method suggested by Malcolm.
[EDIT: In fact, it's definitely not as good as Malcolm's method.]
David. Re: problem with PRINT RND
Post by Malcolm 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.
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.
Re: problem with PRINT RND
Post by Richard Russell on Feb 20th, 2010, 11:01pm
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. Re: problem with PRINT RND
Post by Richard Russell on Feb 20th, 2010, 11:07pm
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.
Re: problem with PRINT RND
Post by Malcolm 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.