Working with the random number generator


+ Reply to Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2007
    Posts
    8

    Question Working with the random number generator

    Newbe here,
    I am working on a project where I can randomly light one of 5 LED attached to portb. PicBasic has a random number generator that will write a random number between 1 and aprox 65K to a variable. So I can divide the vaiable by about 16K to get a number between 0and 4. or by 13k to get a number between 0 and 5..... I can play with that part later. My question is how do I round to a whole number or truncate to a whole number?

    Any help deeply appriciated

  2. #2
    skimask's Avatar
    skimask Guest

    Default

    Quote Originally Posted by kwelna View Post
    Newbe here,
    I am working on a project where I can randomly light one of 5 LED attached to portb. PicBasic has a random number generator that will write a random number between 1 and aprox 65K to a variable. So I can divide the vaiable by about 16K to get a number between 0and 4. or by 13k to get a number between 0 and 5..... I can play with that part later. My question is how do I round to a whole number or truncate to a whole number?

    Any help deeply appriciated
    Are you using PBP? Do you have the manual handy? I've got your answer when you get the manual.

  3. #3
    Join Date
    Jan 2007
    Posts
    8

    Default Random number

    no I am using pic basic, not the pro. I do have the manual, I assume thisis ususing the "//" remianer function? I am not sure how this functions.....

  4. #4
    skimask's Avatar
    skimask Guest

    Default

    Quote Originally Posted by kwelna View Post
    no I am using pic basic, not the pro. I do have the manual, I assume thisis ususing the "//" remianer function? I am not sure how this functions.....
    Do you see anything in the manual about floating point numbers, decimal points, etc.? I.E. 10 / 3 = 3.333333333333 and so on...
    What do you have to truncate for?

  5. #5
    Join Date
    Jan 2007
    Posts
    8

    Default radom number

    Nothing about floating point numbers. My version of pic basic is from when I purchased it in 2002 to make a specialized timer for a friend. I have not used it since if that helps. The manual is quite "thin". What I am truing to do is randomly turn on one of five LEDs tied to portb. I will then test a related porta input tied to a sensor to see when it goes hi or low.....to then generate another random number between 1and 5 to start the process over.

    Here is the actual application, it is for a buddy of mine that has kids in scouting. I0 have a set of steel plates set out at 25 feet each steel plate has an LED mounted below it and a vibtration sensor attahed to it. each scout is given a CO2 pellet pistol loaded with 10 pellets. as each LED lights for a cetain number of seconds - usually 5, the scout needs to shoot the plate, he gets two shots at each plate. If he hits the plate it registers the hit and moves on to the next randomly selected target. At the end of the competition, the elapsed time is taken and the total number of left over pellets multiplied by a constant of usually 5 seconds, this product is then subtracted from the time. This process rewards both speed and accuracy. It is a lot of fun and I am trying to automate the process to make it more challenging. So I need a way to randomly light one of 5 target indiactors. which ever one is lit will then have its vibration sensor monitored for pellet hits.
    Once hit, the led turns off and another one is lit, maybe even the same one relights. The shooter will not be able to guess as it is random..... It should be a hoot to watch them sweat.

    Sorry to be so long winded

  6. #6
    skimask's Avatar
    skimask Guest

    Default

    Quote Originally Posted by kwelna View Post
    Nothing about floating point numbers. My version of pic basic is from when I purchased it in 2002 to make a specialized timer for a friend. I have not used it since if that helps. The manual is quite "thin".
    Wow, that must be quite the manual...How many pages ISN'T is?

    So, if you don't see anything about floating point numbers in the manual, what does that tell you?
    Do you see anything about integers? Do you know what happens to the fractional part of a number when you divide it (if said division ends up with a fractional part) if you're working with only integers?

  7. #7
    Join Date
    Jan 2007
    Posts
    8

    Default random numbers

    It is about 20 pages long. basically it lists the functions available in pic basic and a single line about syntax. in my case it is the "Let" function. So

    Here is the only reference to division:

    {LET} Var = {-} Value { Op Value}

    Assigns a value to a variable. the valuemay be a conmstant, the value of another variable or the result of one or more binary operations. The operations are performed strictly left to right and all operations are performed with 16-bit precision. Unary negation may only be performed on the first value.

    It then lists a number of mathamatical opoerators {+, -, \, \\,....} and some boolean operators { and , or, xand, .....etc}. Nothing about truncating a number, rounding, absolute value, ...etc.

    I think I have a way out, I do the division and instead of truncating and rounding off and look=king for (X = a number 1 to 5) i can test for (X <= to a number). So anything (<= to 1) is 1, (<= to 2) is 2...etc. what do yo think?

  8. #8
    skimask's Avatar
    skimask Guest

    Default

    Quote Originally Posted by kwelna View Post
    It is about 20 pages long. basically it lists the functions available in pic basic and a single line about syntax. in my case it is the "Let" function. So

    Here is the only reference to division:

    {LET} Var = {-} Value { Op Value}

    Assigns a value to a variable. the valuemay be a conmstant, the value of another variable or the result of one or more binary operations. The operations are performed strictly left to right and all operations are performed with 16-bit precision. Unary negation may only be performed on the first value.

    It then lists a number of mathamatical opoerators {+, -, \, \\,....} and some boolean operators { and , or, xand, .....etc}. Nothing about truncating a number, rounding, absolute value, ...etc.

    I think I have a way out, I do the division and instead of truncating and rounding off and look=king for (X = a number 1 to 5) i can test for (X <= to a number). So anything (<= to 1) is 1, (<= to 2) is 2...etc. what do yo think?


    I think you're not getting it...
    PBP doesn't deal with fractions. Fractions are ignored, thrown away, not present, not going to be in the final result, irrelavant...
    In our little world, 10 / 3 = 3 remainder 1, or 3 1/3, or 3.333333333(and so on)

    In PBP's world, 10 / 3 = 3, nothing more, nothing less.

    Do you get it yet?

  9. #9
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,572

    Wink With a little SEARCH

    Hi, Kwelna

    I remember there was a great discussion about random numbers ( here or in the PBP section ) ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  10. #10
    Join Date
    Jan 2007
    Posts
    8

    Talking random numbers

    Oooh now I get it and the useage of the // function. OK so in essence, it always rounds down.....way easier. Sorry to be so thick

Similar Threads

  1. Random Numbers
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 18th December 2008, 08:47
  2. random generator with a limiter
    By kenpo in forum PBP Wish List
    Replies: 11
    Last Post: - 28th April 2007, 10:07
  3. Random number seed
    By kwelna in forum mel PIC BASIC
    Replies: 10
    Last Post: - 1st February 2007, 17:33
  4. Random number results
    By bartman in forum mel PIC BASIC
    Replies: 3
    Last Post: - 9th March 2005, 17:39
  5. more random number questions
    By bartman in forum mel PIC BASIC
    Replies: 1
    Last Post: - 14th November 2004, 17:55

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts