Shop OBEX P1 Docs P2 Docs Learn Events
Random number from 1 to 8 — Parallax Forums

Random number from 1 to 8

ArchiverArchiver Posts: 46,084
edited 2004-05-20 15:42 in General Discussion
I need to select a random number from a range of 1 to 8. I will use
this to generate a time delay.

The random command works from 1 to 65k and needs a seed to start
with. I tried this but all I get is 8 for a y value??? I can see that
X changes to a different number every time that it goes through the
loop but I always get 8 for a Y value????

'{$STAMP BS2p}
'{$PBASIC 2.5}

X VAR Word
Y VAR Word

X = 6789

again:

RANDOM X

IF X <= 8192 THEN Y=1
IF X <= 16384 THEN Y=2
IF X <= 24576 THEN Y=3
IF X <= 32768 THEN Y=4
IF X <= 40960 THEN Y=5
IF X <= 49152 THEN Y=6
IF X <= 57344 THEN Y=7
IF X <= 65535 THEN Y=8

DEBUG DEC y, 13
DEBUG DEC x, 13

GOTO again

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2004-05-19 00:16
    At 11:08 PM 5/18/04 +0000, jbirnsch wrote:
    >I need to select a random number from a range of 1 to 8. I will use
    >this to generate a time delay.
    >
    >The random command works from 1 to 65k and needs a seed to start
    >with. I tried this but all I get is 8 for a y value??? I can see that
    >X changes to a different number every time that it goes through the
    >loop but I always get 8 for a Y value????

    Naturally, the random number will ALWAYS be less than or equal to 65535 which
    is the last test you make. You need to exit the checking, as soon as you make a comparison "hit".


    >'{$STAMP BS2p}
    >'{$PBASIC 2.5}
    >
    >X VAR Word
    >Y VAR Word
    >
    >X = 6789
    >
    >again:
    >
    >RANDOM X
    >
    >IF X <= 8192 THEN Y=1
    >IF X <= 16384 THEN Y=2
    >IF X <= 24576 THEN Y=3
    >IF X <= 32768 THEN Y=4
    >IF X <= 40960 THEN Y=5
    >IF X <= 49152 THEN Y=6
    >IF X <= 57344 THEN Y=7
    >IF X <= 65535 THEN Y=8
    >
    >DEBUG DEC y, 13
    >DEBUG DEC x, 13
    >
    >GOTO again
    >
    >
    >
    >To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    >from the same email address that you subscribed. Text in the Subject and Body of the message will be ignored.
    >
    >Yahoo! Groups Links
    >
    >
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2004-05-19 00:31
    You can do the comparisons in reverse order. This way
    IF X <= 65535 THEN Y=8
    IF X <= 57344 THEN Y=7
    IF X <= 49152 THEN Y=6
    IF X <= 40960 THEN Y=5
    IF X <= 32768 THEN Y=4
    IF X <= 24576 THEN Y=3
    IF X <= 16384 THEN Y=2
    IF X <= 8192 THEN Y=1

    Original Message
    From: jbirnsch [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=ftTX0hzf4ltoYIt878qAqTAqZumUUbY3gp1vZsmJope69yimiagZs4suBLyTWCNmhO2NrKgblil5]jbirnsch@v...[/url
    Sent: Tuesday, May 18, 2004 6:09 PM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] Random number from 1 to 8

    I need to select a random number from a range of 1 to 8. I will use
    this to generate a time delay.

    The random command works from 1 to 65k and needs a seed to start
    with. I tried this but all I get is 8 for a y value??? I can see that
    X changes to a different number every time that it goes through the
    loop but I always get 8 for a Y value????

    '{$STAMP BS2p}
    '{$PBASIC 2.5}

    X VAR Word
    Y VAR Word

    X = 6789

    again:

    RANDOM X

    IF X <= 8192 THEN Y=1
    IF X <= 16384 THEN Y=2
    IF X <= 24576 THEN Y=3
    IF X <= 32768 THEN Y=4
    IF X <= 40960 THEN Y=5
    IF X <= 49152 THEN Y=6
    IF X <= 57344 THEN Y=7
    IF X <= 65535 THEN Y=8

    DEBUG DEC y, 13
    DEBUG DEC x, 13

    GOTO again



    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject
    and Body of the message will be ignored.

    Yahoo! Groups Links
  • ArchiverArchiver Posts: 46,084
    edited 2004-05-19 00:33
    Try the following:

    Turn your X <=.... statements upside down. The highest value at the top.

    Then it should work OK.

    Klaus
    Original Message
    From: jbirnsch <jbirnsch@v...>
    To: <basicstamps@yahoogroups.com>
    Sent: Wednesday, May 19, 2004 1:08 AM
    Subject: [noparse][[/noparse]basicstamps] Random number from 1 to 8


    > I need to select a random number from a range of 1 to 8. I will use
    > this to generate a time delay.
    >
    > The random command works from 1 to 65k and needs a seed to start
    > with. I tried this but all I get is 8 for a y value??? I can see that
    > X changes to a different number every time that it goes through the
    > loop but I always get 8 for a Y value????
    >
    > '{$STAMP BS2p}
    > '{$PBASIC 2.5}
    >
    > X VAR Word
    > Y VAR Word
    >
    > X = 6789
    >
    > again:
    >
    > RANDOM X
    >
    > IF X <= 8192 THEN Y=1
    > IF X <= 16384 THEN Y=2
    > IF X <= 24576 THEN Y=3
    > IF X <= 32768 THEN Y=4
    > IF X <= 40960 THEN Y=5
    > IF X <= 49152 THEN Y=6
    > IF X <= 57344 THEN Y=7
    > IF X <= 65535 THEN Y=8
    >
    > DEBUG DEC y, 13
    > DEBUG DEC x, 13
    >
    > GOTO again
    >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    >
    > Yahoo! Groups Links
    >
    >
    >
    >
    >
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2004-05-19 00:36
    Actually, you can leave the first comparison out... and just set x to 8
    when you declare it.

    Original Message
    From: David W. Sexton [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=RG1OxONOIYLet8LsjvbTHZnvE6BNSDKyyDUpVk1xyl01-elSBK-SxCBpP40J9bpkkxe8JqRj0zkwiVNJ_2CV3yRUT-o]sextondavid@b...[/url
    Sent: Tuesday, May 18, 2004 6:31 PM
    To: basicstamps@yahoogroups.com
    Subject: RE: [noparse][[/noparse]basicstamps] Random number from 1 to 8

    You can do the comparisons in reverse order. This way
    IF X <= 65535 THEN Y=8
    IF X <= 57344 THEN Y=7
    IF X <= 49152 THEN Y=6
    IF X <= 40960 THEN Y=5
    IF X <= 32768 THEN Y=4
    IF X <= 24576 THEN Y=3
    IF X <= 16384 THEN Y=2
    IF X <= 8192 THEN Y=1

    Original Message
    From: jbirnsch [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=HPqr3yKDlEE_WpJYvgRtTB8P4zz0K_mx-UbGXXsM1mV5FFNXR1c_H6k45EyV9_ck090qHpiD]jbirnsch@v...[/url
    Sent: Tuesday, May 18, 2004 6:09 PM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] Random number from 1 to 8

    I need to select a random number from a range of 1 to 8. I will use
    this to generate a time delay.

    The random command works from 1 to 65k and needs a seed to start
    with. I tried this but all I get is 8 for a y value??? I can see that
    X changes to a different number every time that it goes through the
    loop but I always get 8 for a Y value????

    '{$STAMP BS2p}
    '{$PBASIC 2.5}

    X VAR Word
    Y VAR Word

    X = 6789

    again:

    RANDOM X

    IF X <= 8192 THEN Y=1
    IF X <= 16384 THEN Y=2
    IF X <= 24576 THEN Y=3
    IF X <= 32768 THEN Y=4
    IF X <= 40960 THEN Y=5
    IF X <= 49152 THEN Y=6
    IF X <= 57344 THEN Y=7
    IF X <= 65535 THEN Y=8

    DEBUG DEC y, 13
    DEBUG DEC x, 13

    GOTO again



    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject
    and Body of the message will be ignored.

    Yahoo! Groups Links







    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject
    and Body of the message will be ignored.

    Yahoo! Groups Links
  • ArchiverArchiver Posts: 46,084
    edited 2004-05-19 00:40
    The reason that you're always getting 8 is that the final conditional
    test is always true. Here's a simpler way to get that value (I call
    "idx") that won't give you problems and will save some typing:

    Main:
    DO
    RANDOM rndVal
    idx = (rndVal / 8192) + 1
    DEBUG HOME, DEC5 rndVal, " ", DEC idx
    PAUSE 500
    LOOP


    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office


    Original Message
    From: jbirnsch [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=6Zd21mRQY7cNosPmKsJ6utW9leTMmU026iU1flu5Bv8Go_m-8LItJPqp8jN-iI3pX_M44ayV8Q]jbirnsch@v...[/url
    Sent: Tuesday, May 18, 2004 6:09 PM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] Random number from 1 to 8


    I need to select a random number from a range of 1 to 8. I will use
    this to generate a time delay.

    The random command works from 1 to 65k and needs a seed to start
    with. I tried this but all I get is 8 for a y value??? I can see that
    X changes to a different number every time that it goes through the
    loop but I always get 8 for a Y value????

    '{$STAMP BS2p}
    '{$PBASIC 2.5}

    X VAR Word
    Y VAR Word

    X = 6789

    again:

    RANDOM X

    IF X <= 8192 THEN Y=1
    IF X <= 16384 THEN Y=2
    IF X <= 24576 THEN Y=3
    IF X <= 32768 THEN Y=4
    IF X <= 40960 THEN Y=5
    IF X <= 49152 THEN Y=6
    IF X <= 57344 THEN Y=7
    IF X <= 65535 THEN Y=8

    DEBUG DEC y, 13
    DEBUG DEC x, 13

    GOTO again



    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject
    and Body of the message will be ignored.

    Yahoo! Groups Links







    This message has been scanned by WebShield. Please report SPAM to
    abuse@p....
  • ArchiverArchiver Posts: 46,084
    edited 2004-05-20 14:09
    Better still, do this:

    '{$STAMP BS2p}
    '{$PBASIC 2.5}

    x VAR Word
    y VAR Byte

    RANDOM x
    y = 1+ (x & 7)

    Julian

    Original Message
    From:
    sentto-1327227-43884-1084943941-j.porter=computer.org@r...
    om
    [noparse][[/noparse]mailto:sentto-1327227-43884-1084943941-j.porter=computer.org@r...
    .yahoo.com] On Behalf Of K de Jong
    Sent: 19 May 2004 00:33
    To: basicstamps@yahoogroups.com
    Subject: Re: [noparse][[/noparse]basicstamps] Random number from 1 to 8

    Try the following:

    Turn your X <=.... statements upside down. The highest value at the top.

    Then it should work OK.

    Klaus
    Original Message
    From: jbirnsch <jbirnsch@v...>
    To: <basicstamps@yahoogroups.com>
    Sent: Wednesday, May 19, 2004 1:08 AM
    Subject: [noparse][[/noparse]basicstamps] Random number from 1 to 8


    > I need to select a random number from a range of 1 to 8. I will use
    > this to generate a time delay.
    >
    > The random command works from 1 to 65k and needs a seed to start
    > with. I tried this but all I get is 8 for a y value??? I can see that
    > X changes to a different number every time that it goes through the
    > loop but I always get 8 for a Y value????
    >
    > '{$STAMP BS2p}
    > '{$PBASIC 2.5}
    >
    > X VAR Word
    > Y VAR Word
    >
    > X = 6789
    >
    > again:
    >
    > RANDOM X
    >
    > IF X <= 8192 THEN Y=1
    > IF X <= 16384 THEN Y=2
    > IF X <= 24576 THEN Y=3
    > IF X <= 32768 THEN Y=4
    > IF X <= 40960 THEN Y=5
    > IF X <= 49152 THEN Y=6
    > IF X <= 57344 THEN Y=7
    > IF X <= 65535 THEN Y=8
    >
    > DEBUG DEC y, 13
    > DEBUG DEC x, 13
    >
    > GOTO again
    >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    >
    > Yahoo! Groups Links
    >
    >
    >
    >
    >
    >
    >




    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.

    Yahoo! Groups Links
  • ArchiverArchiver Posts: 46,084
    edited 2004-05-20 15:42
    For a much more economical approach, try

    RANDOM x
    y = 1+ (x&7)

    y will be a random number between 1 and 8

    --- In basicstamps@yahoogroups.com, "K de Jong" <klaus0@x> wrote:
    > Try the following:
    >
    > Turn your X <=.... statements upside down. The highest value at the
    top.
    >
    > Then it should work OK.
    >
    > Klaus
    >
    Original Message
    > From: jbirnsch <jbirnsch@v...>
    > To: <basicstamps@yahoogroups.com>
    > Sent: Wednesday, May 19, 2004 1:08 AM
    > Subject: [noparse][[/noparse]basicstamps] Random number from 1 to 8
    >
    >
    > > I need to select a random number from a range of 1 to 8. I will
    use
    > > this to generate a time delay.
    > >
    > > The random command works from 1 to 65k and needs a seed to start
    > > with. I tried this but all I get is 8 for a y value??? I can see
    that
    > > X changes to a different number every time that it goes through
    the
    > > loop but I always get 8 for a Y value????
    > >
    > > '{$STAMP BS2p}
    > > '{$PBASIC 2.5}
    > >
    > > X VAR Word
    > > Y VAR Word
    > >
    > > X = 6789
    > >
    > > again:
    > >
    > > RANDOM X
    > >
    > > IF X <= 8192 THEN Y=1
    > > IF X <= 16384 THEN Y=2
    > > IF X <= 24576 THEN Y=3
    > > IF X <= 32768 THEN Y=4
    > > IF X <= 40960 THEN Y=5
    > > IF X <= 49152 THEN Y=6
    > > IF X <= 57344 THEN Y=7
    > > IF X <= 65535 THEN Y=8
    > >
    > > DEBUG DEC y, 13
    > > DEBUG DEC x, 13
    > >
    > > GOTO again
    > >
    > >
    > >
    > > To UNSUBSCRIBE, just send mail to:
    > > basicstamps-unsubscribe@yahoogroups.com
    > > from the same email address that you subscribed. Text in the
    Subject and
    > Body of the message will be ignored.
    > >
    > > Yahoo! Groups Links
    > >
    > >
    > >
    > >
    > >
    > >
    > >
Sign In or Register to comment.