Shop OBEX P1 Docs P2 Docs Learn Events
Calling an object with a probability of n% — Parallax Forums

Calling an object with a probability of n%

youngdaveyoungdave Posts: 70
edited 2009-08-13 09:19 in Propeller 1
Dear All
I have a loop in Spin repeating infinitely, within which a number of objects are called, one of these needs to be called with a probability of 60%, another with a probability of 0.001%, … etc. Does anybody have any ideas on how to implement this?
TIA David Young

Comments

  • kwinnkwinn Posts: 8,697
    edited 2009-08-12 03:27
    I am assuming that since you say "with a probability of" they have to be called somewhat randomly?

    - Set a threshold for each routine that is proportional to the % times it is to be called.

    - If 0.001% is the smallest then use a random number generator to produce a number between 0 - 1,000,000.

    - The thresholld for 0.001% would be 10, and 60% would be 600,000

    - If the random number is less than the threshold call the routine.

    Another option would be to use a counter with a noisy high frequency input clock instead of a random number generator.
  • SamMishalSamMishal Posts: 468
    edited 2009-08-12 17:14
    Dave,

    I second what Kwin said. But here is a program that achieves it.

    It uses the OBEX object called RealRandom.spin.....see the attached Zip file
    it has all you need. The top level file is called RandomNumber_Demo.spin
    it has commnets for you to see how things are done.

    The RealRandom object uses jitter in the PLL system to create random
    numbers -2147483648 to 2147483647

    I use the Absolute (||) operator to keep it positive
    I also use the modulus (//) operator to keep it from 0 to n-1

    and as Kwin said if you use n of 1000_000 then 60% of the time
    the number will be less than 600_000 and 0.001% of the time it
    will be less than 10.

    I hope this helps

    Samuel

    Post Edited (SamMishal) : 8/13/2009 4:04:22 AM GMT
  • Agent420Agent420 Posts: 439
    edited 2009-08-12 17:20
    OT - The PLL jitter is pretty clever.· Certainly true random numbers are difficult to generate.· So much so, that engineers relied on books such as "A Million Random Digits with 100,000 Normal Deviates", which is just that - a book of 1,000,000 random numbers :-)· You can download the pdf book from RAND.· The Foreward and ·Introduction chapters are·actually quite intersting.

    http://www.rand.org/pubs/monograph_reports/MR1418/
    RAND said...
    This book was a product of RAND's computing power (and patience). The tables of random numbers in the book have become a standard reference in engineering and econometrics textbooks and have been widely used in gaming and simulations that employ Monte Carlo trials. Still the largest known source of random digits and normal deviates, the work is routinely used by statisticians, physicists, polltakers, market analysts, lottery administrators, and quality control engineers.

    A humorous sidelight: The New York Public Library originally indexed this book under the heading "Psychology."


    Post Edited (Agent420) : 8/12/2009 5:29:25 PM GMT
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-08-12 20:34
    Using the modulus operator means to shift the propability. For example:
    The random number generator generates numbers from 0 to 9 but the range you are interested in is 0 - 5. If you now use modulus 6, the result will be 0 for 0 or 6 ... 1 for 1 or 7 ... 2 for 2 or 8 ... 3 for 3 or 9 ... 4 only for 4 and 5 only for 5. This means the propability for 0-3 is different than the propability for 4 and 5.

    If the number generator has a range from 0 to 11 it would be ok to use modulus in this example.

    So, if you need an accurate propability, you should simply regenerate a number in case a number of the upper range is generated.
  • John AbshierJohn Abshier Posts: 1,116
    edited 2009-08-12 21:15
    After you get you random number you need to check it against the cumulative probability distribution of your variable of interest. Lets use for example A with probablity 60%, B with probability 30%, C with probability 9% and D with probability 1%. Use the random number generator and modulus to generate a number between 0 and 99 inclusive, 100 possible numbers; call that integer x. If x < 60 result is A. If x > 59 and < 90, result is B. If x > 89 and < 99, result is C. If x = 99, result is D.

    MagIO2: You example is correct, but the random number generator returns values on the order of 2^31.

    John Abshier
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-08-12 21:36
    @John:
    That's why I said ".. if you need accurate ...". The OP already mentions a propability of 0.001% and that's only an example. So, if the minimum possible propability is even lower, accuracy might be an issue.
  • SamMishalSamMishal Posts: 468
    edited 2009-08-13 04:01
    MagIO2 said...
    Using the modulus operator means to shift the propability. For example:
    The random number generator generates numbers from 0 to 9 but the range you are interested in is 0 - 5. If you now use modulus 6, the result will be 0 for 0 or 6 ... 1 for 1 or 7 ... 2 for 2 or 8 ... 3 for 3 or 9 ... 4 only for 4 and 5 only for 5. This means the propability for 0-3 is different than the propability for 4 and 5.

    If the number generator has a range from 0 to 11 it would be ok to use modulus in this example.

    So, if you need an accurate propability, you should simply regenerate a number in case a number of the upper range is generated.
    I agree with MagI02......but a better way than regenerating the number (which could be very time consuming
    if the number repeatedly comes out to be outside the range) is to do this

    use the || to make it positive and then divide it by ($7FFFFFFF / n)

    so change the line in the program
    ·· Return ||RR.Random // 1000000

    to
    ·· Return ||RR.Random / ($7FFFFFFF / Limit)

    Of course there is a little bit of accuracy loss due to Integer division. However it should be good enough
    for what you are trying to do.

    Samuel


    ·
  • youngdaveyoungdave Posts: 70
    edited 2009-08-13 08:23
    Dear All
    Thanks so much for all the suggestions.
    The PropStick USB I received a couple of weeks ago seems to have been a dud, so I've sent it back to Dave Andreaes for checking.
    With everyone's assistance I've written the rough code, so I'm now treading water until I get the thing back.
    Thanks David Young
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-08-13 09:19
    I have a question to make it clearer to me,

    I assume that the one object is called 60% means from a 100 loops within 60 loops the object should be called
    averaged every 1,667 loops the object should be called REGULARLY ?

    this could be realised with the bresenham-algorythm.
    it was developed for drawing lines with any kind of angle on a screen at high speed
    the algorytm does only addition and substraction if integers and because of this it is fast.
    It is very useful for CNC-controls too if you want two axis move at the same time with different speeds

    best regards

    Stefan
Sign In or Register to comment.