Shop OBEX P1 Docs P2 Docs Learn Events
BS1 Random number from 1000 to 3999 — Parallax Forums

BS1 Random number from 1000 to 3999

Billry1Billry1 Posts: 4
edited 2012-04-13 13:57 in BASIC Stamp
Using only the BS1 chip, what statements are needed to generate a random number from 1000 up to 3999?

THANKS!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-13 09:09
    The BS1's RANDOM statement will generate a sequence of 16-bit unsigned pseudo-random numbers (ranging from 0 to 65535). Look at the description of this statement in the Basic Stamp Manual or the Editor's help files for details. You'll have to do some adjustment of the numbers you get from RANDOM to fit the range you want. You want a range of 3000 starting at 1000. Typically, you'd divide the result of the RANDOM statement by the range you want and take the remainder, then add the starting value. The remainder or modulus operator is "//".
  • Billry1Billry1 Posts: 4
    edited 2012-04-13 09:18
    I'm not a programmer, and don't really want to dedicate hours to learning the process. Do you have a few lines of BS1 working code I can look at and run. Then I can see what the numbers do and swap the figures to make it work for my application. I'm not ashamed to borrow code. THANKS
  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-13 09:39
    You're going to have to learn something about programming a BS1, about entering a program, compiling it, and downloading it to the BS1. What are you really wanting to do? Do you just want a list of "random" numbers displayed on your computer's screen? Remember that these numbers are not really random, they're just "pseudo-random" numbers produced by a linear feedback shift register. They have some properties of random numbers, but they're not really random. In fact, you'll get the same sequence of numbers each time you start your program unless the program is written to add some kind of random event to the mix (like a human initiated keypress).

    To get a pseudo-random number in the range you wanted, you'd do:

    RANDOM variable
    variable = variable // 3000 + 1000

    You'll need some other statements to make a complete program and display the results, but you'll need to look at the DEBUG statement description and a description of how to define variables from the Stamp Manual (Basic Stamp Syntax and Reference Manual). You'll also need to look at the GOTO statement and how to define labels. The "What's a Microcontroller?" tutorial is a good reference for starting to program. It should be in the Editor's help files or you can download it from Parallax.
  • Billry1Billry1 Posts: 4
    edited 2012-04-13 10:02
    The code makes sense, I'll try it. I saw that the other random numbers I produced with the BS1 started to repeat after 20 or so iterations (using the 0-255 set). My device will be totally unattended and without human intervention, once the power comes on, it does its thing over and over and over. Hopefully the numbers I need 1000 to 3999 will be random enough. I've programmed in assembly, Basic, Fortran and Cobol, but that was a couple of decades ago. It may be a sad comment on my life that dabbling in micocontrollers is fun, but maybe profitable. Thanks again for the real code.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-13 10:32
    Actually, the code I showed needs a little change:
    RANDOM variable1
    variable2 = variable1 // 3000 + 1000
    
    The reason is that RANDOM takes the initial contents of the variable and changes it. To get a pseudo-random sequence, you have to use the changed variable value the next time you use RANDOM.
  • Billry1Billry1 Posts: 4
    edited 2012-04-13 13:57
    Got it, thanks again!
Sign In or Register to comment.