Shop OBEX P1 Docs P2 Docs Learn Events
How to produce a real random value between two prescribed values ? — Parallax Forums

How to produce a real random value between two prescribed values ?

kevinspacekevinspace Posts: 56
edited 2011-12-31 09:13 in Propeller 1
Hello everyone~
Are there anyone know that how to produce a real random value between two prescribed values ?
Just like it :
a := random ( Minimum, Maximum )

Are there any example or any suggestion?
Thanks a lot.

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2011-12-29 22:28
    Well ... well ... well ...

    I think the question is a bit to simple to give a good answer!
    1. Can you live with restrictions in which values you can pass as Minimum and Maximum? For example if you can live with the fact that (Maximum - Minimum) has to be a "2 to the power of x"- value ( for example 2,4,8,16,32 ...) then it is easy.
    2. If not, can you live with restrictions in "real randomness"? The problem is that in case you map a 32bit real random number as generated with Chips RealRandom object, which has a equal probability for each number in the 32 bit-range, to a range that does not have 2 to the power of x elements, you loose "equal probability" due to rounding and some values have a slightly bigger probability than others.

    So, if you can live with 1. you simply do:
    (realRandomValue & (Maximum-Minimum-1)) + Minimum
    again .. the allowed values for Maximum is limited to (Minimum + 2^x)

    If you can live with 2. new questions have to be asked:
    What is the max. range? In general it is easy: (realRandomValue / maxRandomValue) * (Maximum - Minimum) + Minimum

    If you really want real random numbers and with any range it's getting complex!
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-12-29 23:20
    Do this:
    value := ||?seed // (max_val - min_val) + min_val
    

    This uses the Propeller's pseudo-random number generator with the global long variable seed. If you need "true" random numbers, you can also use the RealRandom object from the OBEX instead. Although the modulus operator (//) introduces a tiny bit of bias into the results, the effect will be minimal for small values of max_val - min_val.

    -Phil
  • VIRANDVIRAND Posts: 656
    edited 2011-12-30 14:59
    One way to get a perfectly random number (every once in a while, only occasionally) is from reading a counter when a button is pressed
    (and then using Phil's code above to put it in the desired range).

    It's sort of like the observer-influence in quantum randomness. Good for "fair" games of chance, if you were making slots or roulette for example.
  • piguy101piguy101 Posts: 248
    edited 2011-12-30 17:12
    I would go for the button idea if you have a human pushing buttons frequently. I once made a game of simon and rather than using a psudo random generator it was fully based upon how long the user would hold down a button to determine the next color.
  • rogersydrogersyd Posts: 223
    edited 2011-12-31 09:01
    a similar idea is being discussed here and might fit your needs:
    http://forums.parallax.com/showthread.php?136727-Random-Number-between-1-100
  • Heater.Heater. Posts: 21,230
    edited 2011-12-31 09:13
    Have a look at my JKISS32 Random Number Generator post today. Lot's of good randomness there.
Sign In or Register to comment.