My teacher gave us an assignment I need some help.
in BASIC Stamp
Okay, so we made a random number generator, but we keep getting the same numbers every time we run the program. We know that to change the numbers you need to change the seed value, but our teacher won't let us change it every time we run the program. Is there a way to make the program automatically say add one to the seed every time? Thanks any help is appreciated.
Comments
An R and a C on an input pin can be made to give an uncertain number with the RCtime command, especially if you make poor wiring, that is, long and loopy, or use a photosensitive device in place of the R.
How are you generating your random number and does your program run once or keep repeating?
It is best if you can post your program because maybe there is an error in it.
' {$STAMP BS2} ' {$PBASIC 2.5} result1 VAR Word final1 VAR Byte result2 VAR Word final2 VAR Byte counter VAR Byte PulseCount VAR Word result1 = 10 + counter = 1 TO 5 Main: FOR counter = 1 TO 1 final1 =((result1//4) +1) RANDOM result1 DEBUG DEC ? final1 PAUSE 50 NEXT result2 = 10 FOR counter = 1 TO 1 final2 =((result2//4) +4) RANDOM result2 DEBUG DEC ? final2 PAUSE 50 NEXT GOTO IF_Then IF_then: IF (final1 = 3) AND (final2 = 6) THEN FOR counter = 1 TO 23 PULSOUT 13, 850 PULSOUT 12, 850 PAUSE 20 NEXT FOR counter = 1 TO 72 PULSOUT 13, 850 PULSOUT 12, 650 PAUSE 20 NEXT ENDIF
Here is my code.
DEBUG "please press the button to start",13 DO LOOP UNTIL myButtonPin=0 DO RCTIME myButtonPin, 0, PulseCount LOOP UNTIL myButtonPin=1 . ' loops at 131ms intervals until the pin goes high
The idea is that the Stamp waits as long as necessary in the DO loops, until the user has pressed and then released the button. PulseCount will be the last residual value from RCTIME, and that can be the seed.
If the device is connected to a computer, you could ask the user to press a random key on the keyboard, and use that as a seed.
DEBUG "please press random key to start",13 DEBUGIN seed
You could use one of the circuits and code shown on page 364, with a temperature sensor (thermistor) or light sensor (LED connected backwards), to give an environmentally determined seed.
I take it that result1 and result2 will be Random numbers.
Is there a particular range of values they need to be in?
Also, why do you have 2 FOR...NEXT loops that go from 1 to 1 since nothing will change?
Is this code being run on a Boe-Bot because I see you are sending what look to be servo pulses using 2 FOR...NEXT loops.
Also, I am guessing that you want one set to run when the 2 magic numbers come up and then other set to run when they don't, so put an ELSE before the 2nd FOR...NEXT loop.
The GOTO If_Then does nothing since If_Then follows that line and it's good practice to avoid using GOTO.
In addition to the BASIC Stamp Manual, you should also look at the What's a Microcontroller and StampWorks texts.
https://www.parallax.com/sites/default/files/downloads/27218-Web-BASICStampManual-v2.2.pdf
https://www.parallax.com/sites/default/files/downloads/28123-Whats-a-Micro-v3.0.pdf
https://www.parallax.com/sites/default/files/downloads/27297-StampWorks-Manual-v2.1.pdf