Shop OBEX P1 Docs P2 Docs Learn Events
Randon number at start up — Parallax Forums

Randon number at start up

usabob01usabob01 Posts: 15
edited 2014-09-23 16:58 in BASIC Stamp
I
I have been trying to find a way to get a different random number (1 thru 10) every time I start the program. It seems all code I find always starts off with the same number.I want a different number every time I start. Is this possible . Thanks for all Help.
What can I add to this to make it choose from 1 thru 10 rather than hundred's


timeCounter Var Word
do
Loop UNTIL IN12 =1
FOR timeCounter = 1 to10
DO
PAUSE 1
timeCounter = timeCounter +1
Loop UNTIL IN12 =0
DEBUG DEC Time Counter,CR

Sorry I don't now how to paste in this box yet

Comments

  • ercoerco Posts: 20,257
    edited 2014-09-15 20:36
    If you have an extra pin, attach a photocell (old farts like me refuse to call it and LDR) to the BS2. Measure the light level with RCtime. which always changes. With a little math manipulation, you can get a number 1-10 which will be quite random.

    Or you could require a button press to start your program. Measure the duration of the button press and use that as a seed number.
  • usabob01usabob01 Posts: 15
    edited 2014-09-15 21:16
    erco wrote: »
    If you have an extra pin, attach a photocell (old farts like me refuse to call it and LDR) to the BS2. Measure the light level with RCtime. which always changes. With a little math manipulation, you can get a number 1-10 which will be quite random.

    Or you could require a button press to start your program. Measure the duration of the button press and use that as a seed number.
    Great Idea erco thanks so much
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-09-15 22:17
    You can also use Chip's RealRandom object.

    -Phil
  • TtailspinTtailspin Posts: 1,326
    edited 2014-09-15 23:00
    Gee whiz Phil, I'm not sure there's enough COGs in a BS2 for to make Chips RealRandom object work. :)
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-09-15 23:47
    Aw nuts! Wrong forum! :)

    -Phil
  • ercoerco Posts: 20,257
    edited 2014-09-16 01:02
    This is the closest I'll ever get to me being right and PhiPi being wrong.

    Best Day Ever!
  • davejamesdavejames Posts: 4,047
    edited 2014-09-19 09:32
    ...unless I'm missing some subtlety in the request, why not just use the RANDOM command (for a seeded random number) and the '//' binary operator.(to help obtain a number between 1 and 10?

    See the Syntax manual for more info.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-09-19 09:38
    Dave,

    He wants a random seed, so the program behaves differently every time it's run.

    -Phil
  • davejamesdavejames Posts: 4,047
    edited 2014-09-19 09:40
    He wants a random seed, so the program behaves differently every time it's run.

    ...got it.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-09-22 10:16
    The program sits in a loop until the state of P12 is high. Assuming this is a push button operated by a human you actually have the perfect seed randomizer. Just keep doing RANDOM <variable> within the loop. When the button is pressed the value of <variable> will be pretty random unless you're holding the button at the moment the program starts.

    Also, see the Demo Program on page 177 of the BASIC Stamp Manual V2.2 for more additional information.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-09-23 12:51
    Here's a program that generates a random byte using no external hardware (except for your finger).
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    t0  VAR Bit
    t1  VAR Bit
    s   VAR Bit
    i   VAR Nib
    rnd VAR Byte
    
    IF IN15 THEN
      s = 0
    ELSE
      s = 1
    ENDIF
    
    FOR i = 0 TO 7
      DO
        GOSUB GetBit
        t0 = t1
        GOSUB GetBit
      LOOP UNTIL t0 <> t1
      rnd = rnd << 1 + t0
    NEXT
    DEBUG DEC rnd, CR
    END
    
    GetBit:
      DIR15 = 1
      OUT15 = s
      PAUSE 1
      RCTIME 15, s, t1
      RETURN
    

    It works by first detecting the selected pin's (15 in this case) natural input state, then using OUT to flip that state and RCTIME to measure how long it takes to flip back. In order for this to work you have to touch the affected pin (or use a 100+ meg resistor to Vdd or Vss). The least-significant bit of the time it takes will be random. To remove any bias, the program samples two bits at a time. If they're equal, it samples two more until they are unequal. It then shifts the first bit into the result. This process repeats eight times for eight bits.

    -Phil
  • danielstrittdanielstritt Posts: 43
    edited 2014-09-23 15:03
    I'm confused, I thought RANDOM <var> gave a random number between 0 and var. You mean it seeds the random number generator with var? I've been looking for a seeding function lately.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-09-23 16:58
    RANDOM <variable>


    Function: Generate a pseudo-random number.


    Variable is a variable (usually a word) whose bits will be scrambled to produce a random number. Variable acts as RANDOM's input and its result output. Each pass through RANDOM stores the next number, in the pseudorandom sequence, in Variable.


    Explanation:
    RANDOM generates pseudo-random numbers ranging from 0 to 65535. They’re called “pseudo-random” because they appear random, but are generated by a logic operation that uses the initial value in Variable to "tap" into a sequence of 65535 essentially random numbers. If the same initial value, called the "seed", is always used, then the same sequence of numbers is generated.

    via BASIC Stamp Manual V2.2
Sign In or Register to comment.