Shop OBEX P1 Docs P2 Docs Learn Events
random — Parallax Forums

random

ArchiverArchiver Posts: 46,084
edited 2004-05-05 10:57 in General Discussion
could anyone suggest an easy way to produce a random number in the
range 0 - 7 (total of 8 choices). In the manual the Random function
works with variable of a byte or a word. Doing this, I can get
a "randomized" result of 0 255 and then will have to reduce that
down to 0 - 7 range.

Also, the manual talks about pseudo random. How would I make my
number a realy random number of 0 - 7

thanks

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-04-11 04:06
    Good article on truly random numbers in this month's Nuts & Volts magazine.
    They sampled static off a radio and converted it to byte-sized numeric
    values. A bit of overkill for most Stamp stuff though.

    If you want to use the Stamp and only get random numbers from 0-7, just toss
    out all those that are out of range (8-255) or divide the 0-255 random
    number you get by 36. 36 goes into 255 about 7 times, and since the Stamp
    does integer math the result should be 0-7. You can store a value of 0-7 in
    a byte and just take the lower nibble.

    Original Message


    > could anyone suggest an easy way to produce a random number in the
    > range 0 - 7 (total of 8 choices). In the manual the Random function
    > works with variable of a byte or a word. Doing this, I can get
    > a "randomized" result of 0 255 and then will have to reduce that
    > down to 0 - 7 range.
    >
    > Also, the manual talks about pseudo random. How would I make my
    > number a realy random number of 0 - 7
  • ArchiverArchiver Posts: 46,084
    edited 2003-04-11 06:39
    Rodent's suggestion is a reasonable solution, but is not "really random"
    due to the method of producing random numbers in the Stamp. Also,
    sampling radio static adds additional hardware and complexity. A
    software solution is to use long maximal length pseudorandom binary
    sequences (often abbreviated PRBS or just PRS). These are produced by a
    software shift register with exclusive-or feedback from particular
    'taps' in the register. A good explanation is given in the often-cited
    reference, Horowitz and Hill, "The Art of Electronics." Sure, these
    sequences repeat (hence pseudorandom) after shifting the register a
    given number of times (actually 2^n -1, where n is the length of the
    sequence), but these can be quite long. But combinations of them produce
    some really long sequences, such as those exceeding the age of the
    universe in seconds. They are the bases for many of the encryption
    algorithms in present use, with added features of course to prevent code
    breaking.

    As to "really random", that is quite hard to achieve, especially with
    radio static. For example, sun spots (solar storms) occur in cycles and
    are one source of radio static. Radioactive sources might be better,
    producing a Poisson noise distribution, but again implementing these
    requires added complexity and potentially hazardous waste.

    For a range of only 0-7, you might consider creating a table in Stamp
    memory composed of N numbers copied from a published random number
    table, and then use the RAN function one or more times to determine
    where to start reading 8 values from this table.

    Dennis



    Original Message
    From: Rodent [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=4g1hAfaAAo1AZJJs5J4qXCx1ZxLYFHt2hAqsBKFzTeX2nYHb806WF_LHSBO5Vc_EBiLxLmKbHsFZfjHHsg]daweasel@s...[/url
    Sent: Thursday, April 10, 2003 8:07 PM
    To: basicstamps@yahoogroups.com
    Subject: Re: [noparse][[/noparse]basicstamps] random


    Good article on truly random numbers in this month's Nuts & Volts
    magazine. They sampled static off a radio and converted it to byte-sized
    numeric values. A bit of overkill for most Stamp stuff though.

    If you want to use the Stamp and only get random numbers from 0-7, just
    toss out all those that are out of range (8-255) or divide the 0-255
    random number you get by 36. 36 goes into 255 about 7 times, and since
    the Stamp does integer math the result should be 0-7. You can store a
    value of 0-7 in a byte and just take the lower nibble.

    Original Message


    > could anyone suggest an easy way to produce a random number in the
    > range 0 - 7 (total of 8 choices). In the manual the Random function
    > works with variable of a byte or a word. Doing this, I can get a
    > "randomized" result of 0 255 and then will have to reduce that down to

    > 0 - 7 range.
    >
    > Also, the manual talks about pseudo random. How would I make my
    > number a realy random number of 0 - 7




    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.


    Your use of Yahoo! Groups is subject to
    http://docs.yahoo.com/info/terms/
  • ArchiverArchiver Posts: 46,084
    edited 2003-04-11 09:28
    >could anyone suggest an easy way to produce a random number in the
    >range 0 - 7 (total of 8 choices). In the manual the Random function
    >works with variable of a byte or a word. Doing this, I can get
    >a "randomized" result of 0 255 and then will have to reduce that
    >down to 0 - 7 range.
    >
    >Also, the manual talks about pseudo random. How would I make my
    >number a realy random number of 0 - 7

    Another way to restrict the range is with the //8 operator.
    x var word
    y var nib
    do
    random x
    y = x//8 ' gives a pseudo random number 0--7
    debug ?y
    nap 5
    loop

    Always use the random function on a word-size argument, for anything
    like good results.

    If your program involves people pressing pushbuttons, you can grab
    the time it takes them to respond.

    y var nib
    crazy: ' loop her until in0 is low, button pressed
    y=y+1//8 ' this count spins the die very fast
    if in0 then crazy ' loop back so long as in0 is high
    ' come here with y a "random" number

    Many physical measurements have noise on them that you can use to
    randomize. For example, use an RC circuit on a Stamp pin:

    0.1
    +V --; ;--||----o---/\/\---P0
    | | | 1k
    `
    ' |
    |
    com
    /\/\--'
    47k


    x var word
    y var nib
    high 0
    loony:
    RCtime 0,1,y ' about 7 milliseconds, 3500 counts, but lowest
    bits are loony
    y = x//7 ' pick off the lowest bits as your random number
    debug ? y
    goto loony

    For the best, I mean, worst, looniest results, use terrible wiring.
    Connect the top end of the capacitor to the unregulated voltage.
    Make a big loop of wire in the connections around your Basic Stamp
    (which generates lots of switching noise). The worse your wiring,
    the better your random numbers!

    Here is a way to randomize the built-in random function:
    x var word
    y var nib
    high 0
    loonier:
    RCtime 0,1,y ' about 7 milliseconds, 3500 counts, but lowest
    bits are loony
    random x ' next pseudo random number
    x=x+y ' stir it up! no longer pseudo.
    debug ? x//8
    goto loonier



    -- Tracy
  • ArchiverArchiver Posts: 46,084
    edited 2003-04-11 15:14
    "Truly Random" requires reading some natural process
    -- like the ones suggested (Radioactive decay, RF noise).
    Since computers are binary devices, they don't do
    truly random. So, we have 'PseudoRandom' sequences of
    numbers. PseudoRandom number generators are supposed to
    'look' random, and not repeat their sequence for a long
    time -- which is good enough for most purposes.

    --- In basicstamps@yahoogroups.com, "brownstamp" <brownstamp@y...>
    wrote:
    > could anyone suggest an easy way to produce a random number in the
    > range 0 - 7 (total of 8 choices). In the manual the Random
    function
    > works with variable of a byte or a word. Doing this, I can get
    > a "randomized" result of 0 255 and then will have to reduce that
    > down to 0 - 7 range.
    >
    > Also, the manual talks about pseudo random. How would I make my
    > number a realy random number of 0 - 7
    >
    > thanks
  • ArchiverArchiver Posts: 46,084
    edited 2003-04-11 16:53
    > x var word
    > y var nib
    > high 0
    > loony:
    > RCtime 0,1,y ' about 7 milliseconds, 3500 counts, but lowest
    >bits are loony
    > y = x//7 ' pick off the lowest bits as your random number
    > debug ? y
    > goto loony

    Most common bug using RCtime--forgetting to reset it. Should be
    ...
    RCtime 0,1,y ' about 7 milliseconds...
    HIGH 0 ' CANT forget this command to reset it
    ...

    -- Tracy
  • ArchiverArchiver Posts: 46,084
    edited 2004-05-05 10:43
    Hello stampers,

    Sorry for the English being so bad, but I live in de Dutch speaking part of
    Belgium (Flanders)

    For a "lightshow" project I wanted to generate lightning random patterns.

    After playing around a bit with the RANDOM function I saw a repeating light
    pattern.

    It seems that the random function only use a sequence of 15 'random'
    numbers, so after 15 numbers the sequence starts all over again.

    I know that the numbers will be different by assigning another initial value
    to the random variable, but the same numers will always be repeated in a 15
    stage cyclus.

    Is there a "workaround" to increase the "randomability" of this function.
    (creating a large DATA range myself is one possible option but this is very
    memmory consuming.)

    Thx in advance

    Michel De Meester


    Met vriendelijke groetjes,

    > ************************************************
    > Michel De Meester
    > Biotechnisch Onderhoud
    > Universitair Ziekenhuis Antwerpen
    > Wilrijkstraat 10
    > 2650 Edegem - B
    > tel: ++32 (0)3 821 36 47
    > e-mail: michel.de.meester@u...
    >
    > *************************************************
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2004-05-05 10:57
    At 11:43 AM 5/5/04 +0200, De Meester, Michel wrote:
    >Hello stampers,
    >
    >Sorry for the English being so bad, but I live in de Dutch speaking part of
    >Belgium (Flanders)
    >
    >For a "lightshow" project I wanted to generate lightning random patterns.
    >
    >After playing around a bit with the RANDOM function I saw a repeating light
    >pattern.
    >
    >It seems that the random function only use a sequence of 15 'random'
    >numbers, so after 15 numbers the sequence starts all over again.
    >
    >I know that the numbers will be different by assigning another initial value
    >to the random variable, but the same numers will always be repeated in a 15
    >stage cyclus.
    >
    >Is there a "workaround" to increase the "randomability" of this function.
    >(creating a large DATA range myself is one possible option but this is very
    >memmory consuming.)
    >
    >Thx in advance
    >
    >Michel De Meester

    Michel -

    My guess is that you're using a byte sized variable instead of a word sized variable in your RANDOM statement. A word sized variable should give you a range of 0-->65535 in lieu of the present range of 0-->15.

    Your English is excellent, and far better than my Dutch might be!

    Regards,

    Bruce Bates
Sign In or Register to comment.