Shop OBEX P1 Docs P2 Docs Learn Events
Random Bits — Parallax Forums

Random Bits

TtailspinTtailspin Posts: 1,326
edited 2010-12-10 08:48 in Propeller 1
Trying to make a Random four bit Binary pattern, (Think Binary Coded Decimal(BCD))

This Pseudo code hopefully makes my question clear enough...
PUB RandomBits
 
    Bit0 := Random 1 or 0  'Make Bit 1 or 0
    Bit1 := Random 1 or 0  'Make Bit 1 or 0
    Bit2 := Random 1 or 0  'Make Bit 1 or 0
    Bit3 := Random 1 or 0  'Make Bit 1 or 0
 
    RandomBits := Bit0 and Bit1 and Bit2 and Bit3  'to make this >>>> %???? <<<<
The output (RandomBits) goes to a 74154 (4 to 16 line Decoder),
The resulting decoded 0 thru 15 will then light a random led...after being inverted by the 7404's of course.

This Circuits "normal" purpose is a Compass Ring, that uses either the HM55B or the HMC6352 Compass Modules.
However, I wish to make a Random pattern appear, to make a Light Show at start up.

Anyways, I was trying to make a random pattern, and I am to newbie to put all the pieces together...

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-12-08 17:00
    There's a pseudo-random operator (?) in Spin. You initialize a variable (like FOO) to any starting value, then do ?FOO to get a 32 bit "random" number. Use the modulus operator (//) to force that into a range like 0-15 and there you are. You'd have: RandomBits := ?FOO // 16

    Because this is a pseudo-random sequence, it will repeat given the same initial value. That can be handy for debugging. If you need something more "random", you can use some kind of user input or reading the compass 100 or 1000 times and adding the values together to get a starting value. There will usually be some random noise in those values and that will give you a different starting value each time.
  • TtailspinTtailspin Posts: 1,326
    edited 2010-12-08 19:50
    I was trying to use the "?" with the limit Max <# and Min #>, and completely overlooked Modulus..Sigh...
     
    [LEFT]PUB RandomBit  | Bit0, Bit1, Bit2, Bit3, Rand[/LEFT]
        Rand := 10
        Bit0 := ?Rand // 1 
        Bit1 := ?Rand // 1
        Bit2 := ?Rand // 1
        Bit3 := ?Rand // 1
     
    '    Combine RandomBits   Bit0 and Bit1 and Bit2 and Bit3  'to make this >>>> %???? <<<<
     
        RandomBits :=  'What should this line of code look like?
     
    
    Still not sure how to combine all four of the "Bits" into one though. What should that code look like?
    Do I shift something?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-12-08 20:01
    X // 1 will always return zero (or should anyway). X // 2 is what you want. But, better yet, use X & 1.

    -Phil
  • kuronekokuroneko Posts: 3,623
    edited 2010-12-08 20:03
    Ttailspin wrote: »
    Still not sure how to combine all four of the "Bits" into one though. What should that code look like?

    Do you need the bits as such? If not just extract 4 bits from the LFSR, e.g. nibble := ?Rand & %1111. Otherwise it's
    Bit3 << 3 | Bit2 << 2 | Bit1 << 1 | Bit0
    
  • TtailspinTtailspin Posts: 1,326
    edited 2010-12-08 21:48
    This stuff is so easy, it's hard...
    nibble := ?Rand & %1111.
    
    Yes, I want the RandomBits to look like this %???? because I do a lot of this...
    PUB CountUP(Time, FlashRate) 'FlashRate = 1 to 2000+   'Sets Speed of Light Show   'More = Faster
     
        dira[rudderA..rudderD] := %1111         'Set up 74154 Pins
     
      repeat time
     
        outa[rudderA..rudderD] := %0000  '0
          waitcnt(clkfreq/FlashRate + cnt)
     
        outa[rudderA..rudderD] := %1000  '1
          waitcnt(clkfreq/FlashRate + cnt)
     
        outa[rudderA..rudderD] := %0100  '2
          waitcnt(clkfreq/FlashRate + cnt)
     
        outa[rudderA..rudderD] := %1100  '3      'Binary Coded Decimal from 0 to 15 
        'ect,ect...
    
    This BCD works with CD4028's too...as I am sure You are aware.

    I know 74HC595's are more "effective" for the Compass Ring, I have that up and running allready..
    But I got to thinking that maybe, one 74HC595 could control two CD4028's for 20 led's, or two 74154's, or...oh my...

    Anyways, thanks for the speedy answers from everybody, I will chew on this for day's...
  • potatoheadpotatohead Posts: 10,261
    edited 2010-12-08 22:33
    Know the "&" operator works on a simple rule; namely, put a 1 in the result, when there are ones in both arguments; otherwise, output a zero. This is evaluated for all the binary digits at one time.

    The bit of code you have above works by simply allowing only the lower 4 binary digits to contain a value. %1111_1111 & %0000_1111 = %0000+1111

    %1111_1111 & %0110_1111 = 0110_1111, and so on.

    There are truth tables in the forums here, and elsewhere online. These are well worth learning, because a lot of short cuts depend on the bitwise operators.
  • TtailspinTtailspin Posts: 1,326
    edited 2010-12-09 10:10
    This works for me, Thanks for all tip and pointer's
    PUB RandomLed(FlashRate, Time)              ''Flash Random Led's on 74154
        dira[rudderA..rudderD] := %1111          'Set up 74154 Pins
        repeat  Time                             'How many Time's to repeat
          outa[rudderA..rudderD] := RandomBits   'Set A B C D pin's to Random Nibble
          waitcnt(clkfreq/FlashRate + cnt)       'Wait as long as needed for Cool Light Show
     
    PUB RandomBits  | Rand, RandNibble          ''Makes Random Nibble for Binary Coded Decimal
     
        Rand := ++Rand                     'Increment Rand to prevent detectable repetition of led's
        RandNibble := ?Rand & %1111              'Makes Random Nibble then AND's with %1111 mask
        return RandNibble                        'Returns Random Nibble 
    

    The Truth Tables will set You free...
  • ElectronegativityElectronegativity Posts: 311
    edited 2010-12-09 19:44
    I like to do randomization in hardware.
    All you need is one pin, one resistor, and one capacitor.

    1. Put the capacitor and resistor in parallel between a pin and ground choosing values so that it will take at least 1000 clock cycles for the capacitor to drain through the resistor.
    2. Set the pin to output and pull it high.
    3. Set the pin to input and count the number of clock cycles it takes to transition from high to low.
    4. Take the least significant bit of this counter as a coin flip.

    Once you have coin flips you can generate any random number.

    Thermal variations and antenna effects from the traces effectively randomize the precise clock cycle on which the logical transition occurs.

    This may seem crazy when you have a pseudo random number generator available, but if you start with the same seed value every time you will get the same sequence of numbers out of the generator.
    If you have human input then you can seed with the clock value at the time of the human interaction and you are good to go, but if you want to turn something on and have it do random things without any additional input then you need to have hardware randomization.
  • TtailspinTtailspin Posts: 1,326
    edited 2010-12-10 08:48
    ^^^^^ The Hardware solution would be for sure more random then using the same seed value over and over.

    But in this particular case, It would be a bit of an "overkill", as I just needed the Illusion of randomness..

    I know that I can get a very random number by just changing the seed value when i use "?", and a way
    to do that is to "spin" the seed number up or down until a button us pushed, humans just aren't accurate
    enough to push the button at the exact same time everytime..(well most humans anyway...)
    But I do understand what You are saying if I don't have a Human to push the button..

    My goal here is "Nibble Control" and this thread has brought much to light in that direction..

    Thanks again for any and all info.. Just a single word is all it takes sometimes..
Sign In or Register to comment.