I need code to generate random number from 1 to 7 in PICBASIC PRO. How to? Thanks.
I need code to generate random number from 1 to 7 in PICBASIC PRO. How to? Thanks.
Might not be the best way, but here is one way (untested):
Code:random_word var word randomdig var byte generate_random: Random random_word 'generate random number and put into random_word var randomdig = random_word DIG 1 'read only first digit if randomdig = 8 then goto generate_random 'if its 8 then do over if randomdig = 0 then goto generate_random ' if its 0 then do over
To filter, you can also use:
if randomdig > 7 or randomdig < 1 . . .
Russ
Russ
N0EVC, xWB6ONT, xWN6ONT
"Easy to use" is easy to say.
PBP's RANDOM command is FAR from RANDOM.I need code to generate random number from 1 to 7 in PICBASIC PRO. How to? Thanks.
Makes good pink noise though ...
But anywho ...
Code:RANDOM RandVar Result = RandVar ** 7 + 1
DT
Pretty FAR!
It's a REPEATING sequence of 65535 numbers.
If you start with the same "Seed" each time ...
You will get the exact same numbers every time you run your program.
If you start with a different seed, all that does it put you somewhere in the middle of that same repeating sequence.
There's really nothing "RANDOM" about it.
It's called pseudo-random, but even that's a stretch.
<br>
DT
OK.....
What is the best way to approach the need for the appearance of "random"?
I have seen that there is a pattern and haven't figured out a way to circumvent it other than to make the sequence long to make it harder to see the pattern (LEDs)
Mark
This is an older post I searched because I need to generate a random number and saw you can do it with a word variable. Like this fella, I need a smaller number.
I'm having a hard time grasping what is going on with Result = ** 7 + 1
Huh ?
Saw --
** Returns the high 16 bits of a 16-bit multiply result
But even that isn't making sense to me.
Has there been any updates to random where you can apply it to more than a word? -thanks
That line of code will scale the WORD sized Randvar variable (0 to 65535) to a value ranging from 1 to 7.
Bascially it's the same as doing Result = Randvar * 7 / 65536 + 1
/Henrik.
Use biased zener connected to ADC and sample the input![]()
Bookmarks