Shop OBEX P1 Docs P2 Docs Learn Events
Random Bits — Parallax Forums

Random Bits

searchfgold6789searchfgold6789 Posts: 6
edited 2010-11-12 19:14 in BASIC Stamp
I am having trouble understanding the RANDOM command in Basic Stamp Programming.

I am trying to simply generate a random number from 0 to 1, store it in a variable, and do it again in the same variable.

Is this possible?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-11-10 14:38
    The RANDOM statement takes a 16 bit integer and transforms it into another 16 bit integer in a way that has some of the properties of a sequence of random numbers, but is really deterministic. In your case, you want a random value between 0 and 1 which is not really doable given that all arithmetic on a Stamp is done on 16 bit integers, so there is no such thing as a floating point or fixed point value (other than integers).

    When people need fractional values on an integer-only device, they usually use scaled arithmetic where they use integers to represent values with decimal points. For example, you can use 1534 to represent the fixed point value 15.34. The Stamp doesn't keep track of the number of decimal places. You have to do that and you have to explicitly provide for scaling when you multiply or divide these values together or change the number of decimal places. You also have to be careful of overflows since all arithmetic (including intermediate results) is done using 16 bit values.

    What do you want to do with your "random" number?
  • searchfgold6789searchfgold6789 Posts: 6
    edited 2010-11-10 17:48
    Well I want to take my number which is either 0 or 1 (high or low), and put it in a variable, which is read and output PINS are turned on or off depending on whether or not the value is 1 or 0. Then the process is repeated 50000 times in a FOR...NEXT block, twenty times within another FOR...NEXT block. I will also DEBUG different strings depending on the result of the number.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-11-10 20:03
    OK, so you want an integer value of 0 or 1 that's randomly (or pseudo-randomly) distributed.

    You do that by using the RANDOM statement on a variable, say Rand, and then using (Rand >>15) for your value.

    Rand will be in the range 0-32767 half the time and in the range 32768-65535 half the time. (Rand >> 15) is a 0 for the range 0-32767 and a 1 for 32768-65535.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-11-10 23:08
    Another way to obtain psuedorandom bits is with a LFSR: linear feedback shift register. This does not use the RANDOM command. I'm still not sure if you want a whole variable (byte or word) to light a bank of LEDs, or one bit at a time.
    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    randy VAR WORD   ' 16 bits for the shift register
    newbit0 VAR randy.BIT0  ' its bit0 will be the random bit
    
    main:
      randy = $5555 ' seed the sequence
      DO
        GOSUB taps
        DEBUG BIN1 newbit0
        PAUSE 16  ' do other stuff with the bit
      LOOP
      END
    
    taps:   ' lfsr random bit generator
      randy=(randy<<1)+(randy.BIT15^randy.BIT14^randy.BIT12^randy.BIT3)
    RETURN
    
  • searchfgold6789searchfgold6789 Posts: 6
    edited 2010-11-11 05:55
    Thank you! the program is working the way I want it to now.
  • searchfgold6789searchfgold6789 Posts: 6
    edited 2010-11-12 06:35
    Well now I am getting a confusing error... When I try to run
    '{$STAMP BS2}
    heads VAR WORD
    tails VAR WORD
    forum VAR WORD
    'recount VAR BYTE
    flip VAR WORD
    heads = 0
    tails = 0
    flip = 0

    'FOR recount = 1 TO 20

    FOR forum = 1 TO 50000
    DEBUG HOME
    RANDOM flip
    IF (flip >> 15) < 32767 THEN
    DEBUG "Heads"
    LOW 3
    HIGH 2
    heads = (heads + 1)
    ELSE
    DEBUG "Tails"
    LOW 2
    HIGH 3
    tails = (tails + 1)
    END IF
    NEXT
    DEBUG CR, ? heads, CR
    DEBUG ? tails
    'NEXT

    LOW 2
    LOW 3
    END

    I get
    Error: Tokenization of /home/richie/Projects/BStamp/1000000CoinToss.bs2 failed.
    Failed compile: 148-Expected a label
    The following text gave the error:
    
    What does that mean?
  • Mike GreenMike Green Posts: 23,101
    edited 2010-11-12 08:24
    There are two versions of Parallax Basic, 2.0 and 2.5. The IF statement particularly is very restricted in version 2.0 and ELSE and ENDIF are not allowed. Since version 2.0 is the default for the Stamp Editor, you need to add a "' {$PBASIC 2.5}" directive to your program. See page 123 of the Stamp Manual.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-11-12 19:14
    Also, this statement
    RANDOM flip
    IF (flip >> 15) < 32767 THEN
    
    should be changed to either
    RANDOM flip
    IF flip < 32768 THEN
    
    or
    RANDOM flip
    IF (flip >> 15) = 0  THEN
    


Sign In or Register to comment.