Shop OBEX P1 Docs P2 Docs Learn Events
random numbers in pbasic? — Parallax Forums

random numbers in pbasic?

RontopiaRontopia Posts: 139
edited 2006-12-07 15:46 in Robotics
Im still new to basic so let me discribe my issue.

I have a boebot that is roaming with a sonic sensor. I wrote all this code myself and its working quite well. when this program is done I will post it here for others to look at and give feedback on.

right now, my bot goes forward with the sonic sensor pinging away. every so often, the bot stops and the the sonic sensor pings in 3 different directions and saves the values. depending on what those values are the bot turns and gos on its way..

so there is a value in 3 drections but if there is an object in the forward direction and nothing to the sides.. ie· 0 1 0 .. I have the bot truning to the 15 degrees·right and starting forward on its way again. what i would like to happen is.. the the case of 0 1 0.. I would like a random 50/50 chance to trun right or left.. and contunue on its way..

problem is I have no idea how to do this in pbasic. I understand how and where I need to branch and return to.. but as far as the random 50/50 chance im clueless. I am assuming that I need some kind of counter or something but how to stop it randomly is the real quesion? after that its simple evaluation to get to the result..

anyway.. does anyone have any ideas or pointers?

thanks for your time



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Muahdib


IC layout designer
Phoenix Arizona

Post Edited (Muahdib) : 12/6/2006 4:20:42 PM GMT

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2006-12-06 02:09
    Take a look at PBASIC's RANDOM operator. You can then "flip a coin" by determining whether the random variable is greater than 32767 or not.

    -Phil
  • RontopiaRontopia Posts: 139
    edited 2006-12-06 02:15
    ok.. sorry, I actually went and did some reading and found that there is a random key word.. its that nice[noparse]:)[/noparse] but I am having trouble with syntax.. so for any help I would be most thankful

    so tell me if there is anything wrong with this bit of code

    Randomnum:
    IF counter2 > 14 THEN
    counter = 0
    counter2 = rvalue
    ENDIF

    RANDOM rvalue
    counter2 = counter2 + 1
    IF rvalue > 32767 THEN
    GOSUB TurnR
    ELSE
    GOSUB TurnL
    ENDIF
    RETURN



    I have made 2 varibles one is a nib and one is a word. Im using the nib to count from 0 to 14 . making that value the seed value

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Muahdib


    IC layout designer
    Phoenix Arizona
  • RontopiaRontopia Posts: 139
    edited 2006-12-06 02:19
    opps its actually counting 0 to 15.. my bad.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Muahdib


    IC layout designer
    Phoenix Arizona
  • RontopiaRontopia Posts: 139
    edited 2006-12-06 02:23
    and that 2nd counter should be counter2.. im at work guys and I got so much going on that I cant focus, if you cant tell by my posts..

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Muahdib


    IC layout designer
    Phoenix Arizona
  • ZootZoot Posts: 2,227
    edited 2006-12-06 20:52
    Since random numbers are only "pseudo-random" on the Stamp, you might want to seed your random call with a number that changes a lot -- randomly. I often use one of my sensor values and maybe a counter too -- your sonar sensor will be returning slightly different values all the time and can make a decent pseudo random seed.

    someSensor  VAR  Word    'some changing value ---
    work             VAR  Word     'work for random (16 bit)
    counter         VAR   Word   'or byte or nib or any counter or index in your program that changes a lot
    
    RandomDir:
       GOSUB Get_Rand    'this subroutine returns work as a 16 bit random number
       IF work.BIT0 = 0 THEN     'just use bit0 -- the number has to be even or odd
           GOSUB TurnL
        ELSE
           GOSUB TurnR
        ENDIF
        RETURN
    
    Get_Rand:         'nice to have a standalone random number generator for all kinds of things
        work = SomeSensor*counter   'or other "seed value" that changes often during other operations in your program
        RANDOM work
        RETURN
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • RontopiaRontopia Posts: 139
    edited 2006-12-06 23:09
    your right.. the code I wrote there will trun left then right then left then right.. the reason I did it this way is so that I only want to use 1 word varable because ram is so limited. but I think I can work with your idea. I dont understand the work.BIT0 statement? .BIT0 refers to what bit?


    thanks.. for the input..

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Muahdib


    IC layout designer
    Phoenix Arizona
  • ZootZoot Posts: 2,227
    edited 2006-12-07 13:50
    Bits are numbered 0 - 15 in a Word and 0 - 7 in a Byte, etc., with 0 being the right-most or (L)east (S)ignificant (B)it and -- in this case -- 15 being the left-most or (M)ost (S)ignificant (B)it.

    So grabbing BIT0 is the same as saying Value//2. BIT0 will be 0 if the number is even and 1 if the number is odd, in other words. Now, for your particular random scenario, you could actually grab *any* bit.

    You could also do something similar with a lookup table, or an ON statement, whatever:

    RandomTurn:
    RANDOM work 'some random value
    ON work.BIT0 GOSUB TurnL, TurnR
    RETURN
    
    TurnL:
    'some left turn code that you can use all over your program
    RETURN
    
    TurnR:
    'some right turn code -- ditto
    RETURN
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    Post Edited (Zoot) : 12/7/2006 1:55:18 PM GMT
  • AImanAIman Posts: 531
    edited 2006-12-07 13:51
    The way I did random numbers was to use numbers I wouldn't know. The first was equal to my encoder / lapsed seconds and the second was seeded with the first. So in english it goes like this

    Random generator = (time in seconds/encoder wheel count)*2
    if random generator is even then turn left
    if random generator is odd the turn right
  • ZootZoot Posts: 2,227
    edited 2006-12-07 13:55
    AIman is doing something similar to what I suggested -- use a value that is a constantly used and changing variable for your seed -- a sensor input, a program counter, an encoder, whatever.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • RontopiaRontopia Posts: 139
    edited 2006-12-07 15:46
    wow thanks guys.. thats just to cool. I think I got it now.. well almost but I think I know enough to be dangerous[noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Muahdib


    IC layout designer
    Phoenix Arizona
Sign In or Register to comment.