Shop OBEX P1 Docs P2 Docs Learn Events
RANDOM Number for a Simple Program — Parallax Forums

RANDOM Number for a Simple Program

ShaneKShaneK Posts: 9
edited 2012-12-17 08:47 in BASIC Stamp
Hello,

I have a simple little guessing game right here and I am wondering how to get a Random number.
Whenever I use the RANDOM variable command I get the same value.
I am really confused on what to do. Any help would be appreciated.

Thanks,

Shane K.
' -------[ Variables ]------------------------------------------------------------------------

RANDOMNumber VAR Byte
INPUTNumber VAR Word
guesses VAR Byte

' -------[ Initialization ]-------------------------------------------------------------------

' -------[ Main Code ]------------------------------------------------------------------------
RANDOM RANDOMNumber

DEBUG CLS

DO
   DEBUG "Please choose a number!", CR

   DEBUG DEC RANDOMNumber, CR                 ' Delete this for real game.
   DEBUGIN INPUTNumber

   DO
   LOOP UNTIL IN2 = 1

   IF INPUTNumber > RANDOMNumber THEN
      DEBUG CR, "That number is too high!", CR
      guesses = guesses + 1
      DEBUG "You have guessed ", DEC guesses, " times.", CR
   ELSEIF INPUTNumber < RANDOMNumber THEN
      DEBUG CR, "That number is too low!", CR
      guesses = guesses + 1
      DEBUG "You have guessed ", DEC guesses, " times.", CR
   ELSEIF INPUTNumber = RANDOMNumber THEN
      DEBUG CR, "Good job! The number was ", DEC RANDOMNumber, " and you had ", DEC guesses, " guesses.", CR
   ELSEIF INPUTNumber = 10000 THEN
      DEBUG CR, "Of course Master Shane. You are better than Chuck Norris.", CR
   ENDIF
   PAUSE 2000
LOOP

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-12-14 10:02
    Read the section of the BASIC Stamp Syntax and Reference Manual on the RANDOM statement. As you've noticed, this doesn't produce a random number, only one that behaves somewhat (statistically) like a random number. RANDOM takes the current contents of its parameter (RANDOMNumber in your case - which starts at zero) and runs it through a "linear feedback shift register" or LFSR. If you want a sequence of values that's "more random", you have to start with a random or a non-deterministic event. You could have a pushbutton attached to your stamp and have the user push it and time how long it's pushed ... that's a non-deterministic event. Use that time to initialize RANDOMNumber and you'll get what appears (and behaves statistically) much like a random sequence. There's a routine for the Propeller that generates random numbers by looking at some of the internal chip timing which is non-deterministic and temperature sensitive as well. Unfortunately, you can't do that with the Stamps. You have to use some external information.
  • ShaneKShaneK Posts: 9
    edited 2012-12-14 16:57
    Mike Green wrote: »
    Read the section of the BASIC Stamp Syntax and Reference Manual on the RANDOM statement. As you've noticed, this doesn't produce a random number, only one that behaves somewhat (statistically) like a random number. RANDOM takes the current contents of its parameter (RANDOMNumber in your case - which starts at zero) and runs it through a "linear feedback shift register" or LFSR. If you want a sequence of values that's "more random", you have to start with a random or a non-deterministic event. You could have a pushbutton attached to your stamp and have the user push it and time how long it's pushed ... that's a non-deterministic event. Use that time to initialize RANDOMNumber and you'll get what appears (and behaves statistically) much like a random sequence. There's a routine for the Propeller that generates random numbers by looking at some of the internal chip timing which is non-deterministic and temperature sensitive as well. Unfortunately, you can't do that with the Stamps. You have to use some external information.

    Great! Thank you, that helps alot I think I will do as you told me!

    Another quick question on an unrelated note, which propellor kit should I get if I am a high school student working on the BS2 Board of
    Education kit at the moment.

    I am looking for something somewhat similar to what I find with the BS2 Board of Education that comes with a guide as the BoE does.

    The two choices are:

    Propeller Education Kit – PropStick USB Version

    or

    Propeller Board of Education

    Thank you,
    Shane K.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-12-14 21:29
    The Propeller Board of Education comes with a lot of stuff already and you can substitute it for the Board of Education when you want. The equivalent to the BS2 Board of Education documentation still isn't developed as much. There's supposed to be a "What's a Microcontroller?" equivalent eventually, but there is the PEK Tutorials and the new <learn.parallax.com> website.
  • ShaneKShaneK Posts: 9
    edited 2012-12-15 12:09
    Mike Green wrote: »
    The Propeller Board of Education comes with a lot of stuff already and you can substitute it for the Board of Education when you want. The equivalent to the BS2 Board of Education documentation still isn't developed as much. There's supposed to be a "What's a Microcontroller?" equivalent eventually, but there is the PEK Tutorials and the new <learn.parallax.com> website.

    So would you suggest that I get the Propeller Board of Education? And then just use the tutorials on the website?

    Also does the kit come with like all the little components such as LED's, resistors, capacitors and such?

    Thanks,

    Shane K.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-12-15 19:42
    No, there's no kit yet for the Propeller Board of Education. You can get the add-ons for the Stamp Board of Education kit here, but you probably have the book already and it applies only indirectly to the Propeller. You can buy the Propeller Education Kit Labs parts as a kit that you could use with any of the Propeller boards including the Propeller Board of Education. See here.
  • ShaneKShaneK Posts: 9
    edited 2012-12-17 08:47
    Mike,
    Thank you for all of your help, I really appreciate it! I haven't quite decided what I am going to do but I am excited for everything that you guys will send me!!
    Shane K
Sign In or Register to comment.