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

Random number

edited 2005-10-14 21:58 in BASIC Stamp
Hi out there, I'm trying to make a radom number generator with five outputs,· this is to blink lights in a radom matter, being new at this, would beg for some help.
Thank you

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Old redneck hillbilly born and raised on a redwood stump

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-13 02:16
    You didn't say if you want all lights to be on it once? Is that what you want, or do you want just one (of the five) light on at any give time. Let me know what you want and I'll whip up a little program for you.· And what BASIC Stamp are you using?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • edited 2005-10-14 17:30
    Thank you so much Jon for your interest, you have helped me so much and its appreciated.· I sent the info to your E-mail, I have over twenty light displays in quick basic from years ago while in programing class, should anyone wish to convert them to the stamp, again tks so much.
    Bill Horn
    Crescent City
    bill.horn@charter.net

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Old redneck hillbilly born and raised on a redwood stump
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-14 18:00
    Didn't I convert those programs for you back in 1995? If you sent an e-mail to me recently I didn't get it or it was accidentally deleted (I get bombed with lots of personal requests). Let's share this fun topic with everyone; post your old code here and I'll reconvert.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • edited 2005-10-14 20:22
    Yes you did convert one for me and still use it, thank you and
    here is the old code, would like it for BS1.


    port = &H378

    RANDOMIZE TIMER

    ct = 0

    DO UNTIL ct = 5

    FOR CR = 1 TO 31

    r = INT(31 * RND + 1)

    FOR I = 1 TO C * 2.5 'DELAY

    NEXT I

    LOCATE 6, 30: PRINT "TWENTY ONE "

    LOCATE 8, 30: PRINT "RANDON ALL "

    IF r = 1 THEN

    OUT port, 1

    ELSEIF r = 2 THEN

    OUT port, 2

    ELSEIF r = 3 THEN

    OUT port, 3

    ELSEIF r = 4 THEN

    OUT port, 4

    ELSEIF r = 5 THEN

    OUT port, 5

    ELSEIF r = 6 THEN

    OUT port, 6

    ELSEIF r = 7 THEN

    OUT port, 7

    ELSEIF r = 8 THEN

    OUT port, 8

    ELSEIF r = 9 THEN

    OUT port, 9

    ELSEIF r = 10 THEN

    OUT port, 10

    ELSEIF r = 11 THEN

    OUT port, 11

    ELSEIF r = 12 THEN

    OUT port, 12

    ELSEIF r = 13 THEN

    OUT port, 13

    ELSEIF r = 14 THEN

    OUT port, 13

    ELSEIF r = 15 THEN

    OUT port, 15

    ELSEIF r = 16 THEN

    OUT port, 16

    ELSEIF r = 17 THEN

    OUT port, 17

    ELSEIF r = 18 THEN

    OUT port, 18

    ELSEIF r = 19 THEN

    OUT port, 19

    ELSEIF r = 20 THEN

    OUT port, 20

    ELSEIF r = 21 THEN

    OUT port, 21

    ELSEIF r = 22 THEN

    OUT port, 22

    ELSEIF r = 23 THEN

    OUT port, 23

    ELSEIF r = 24 THEN

    OUT port, 34

    ELSEIF r = 25 THEN

    OUT port, 25

    ELSEIF r = 26 THEN

    OUT port, 26

    ELSEIF r = 27 THEN

    OUT port, 27

    ELSEIF r = 28 THEN

    OUT port, 28

    ELSEIF r = 29 THEN

    OUT port, 29

    ELSEIF r = 30 THEN

    OUT port, 30

    ELSEIF r = 31 THEN

    OUT port, 31

    END IF

    NEXT CR

    ct = ct + 1

    LOOP

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Old redneck hillbilly born and raised on a redwood stump
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-14 20:40
    I've attached code that will work for you.· It's currently running on my Prop-1 (BS1) and lighting LEDs on the Prop-1 Trainer Board.· All you have to do is adjust the·timing.

    For those wondering why RANDOM is tossed three times ... we found this gives a more visually-interesting display when used in this kind of program.· RANDOM uses an LFSR (linear feedback shift register) and the nature of that design causes us to "see" the shifting of bits through it when RANDOM is called just once.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • edited 2005-10-14 21:34
    Jon you are some kind of a genius, it works so well, maybe now seeing how you did it I can venture out on my own. What baffles me is you replaced 50 lines of code with 5.
    Again tks so much

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Old redneck hillbilly born and raised on a redwood stump
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-14 21:58
    Truth be told, you could have used the same strategy with your PC program:

    · OUT port, r

    ... could have replaced all those IF-THEN constructs as you had already limited r to 1..31.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.