Random Binary Sequence

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cboth
    New Member
    • Jun 2006
    • 2

    Random Binary Sequence

    Hello everyone! I'm searching for a Random Binary Sequence which creates the
    outcome of an XOR gate. I need to create a sequence of 2^(7) -1 bits....using my
    C compiler. Thank you all for your time...
    Last edited by cboth; Jun 29 '06, 10:25 PM.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Sorry ut that doesn't make sense to me, since XOR is definately not random I fail to see how a Random Binary Sequence could be the output of XOR.

    Perhaps you could explain the problem in more detail.

    Comment

    • cboth
      New Member
      • Jun 2006
      • 2

      #3
      Thanx for your interest. I need to create a Random Binary Sequence starting with
      four '1' (bits), that is to say 1 1 1 1. We now assume that the two last bits are inputs of an XOR gate. We calculate their outcome which is 0. The next sequence we receive is 0 1 1 1, which results from the movement of 0 into the first position and the movement of the three first bits by one position to the right, just like if we had a shift register. We are doing this procedure many times and at last we receive the last column. I mean that each time we need to keep only the last of the four
      bits. Now, if we need to receive 2^(7)-1 bits we must calculate 127 times the outcome of the XOR gate and receive each time the last bit, that is to say
      127 bits.
      Hope not to ask too much!!!! I think that such a function exists on Basic, but I
      need to do it in C. Thanks again!!! :-)

      Comment

      • D_C
        Contributor
        • Jun 2006
        • 293

        #4
        The following code is a terrible way to do this, using only XOR for the booleans. There's a much nicer way to do it, I'll leave it up to you to figure out what the code is doing, and how to make it nicer.
        Code:
        int main()
        {
          bool input[] = {true,true,true,true};
          int i;
        
          for(i = 0; i < 127; i++)
          {
            input[0] ^= input[1];
            input[1] ^= input[0];
            input[0] ^= input[1];
            input[2] ^= input[3];
            input[3] ^= input[2];
            input[0] ^= input[2];
            input[2] ^= input[0];
            input[0] ^= input[2];
        
            if(input[0])
              cout << "1";
            else
              cout << "0";
          }
          cout << endl;
          system("PAUSE");
          return EXIT_SUCCESS;
        }
        Ignoring the spaces, the result is: 000100110101111 repeated eight and a half times. I think chaotic is a better word than random.
        Last edited by D_C; Jun 30 '06, 09:31 PM. Reason: 8.5 times, not 7.5

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          OK well I think something like this is what you want

          Code:
          #include "stdio.h"
          
          
          int main(int argc, char* argv[])
          {
              unsigned char data = 0xff;
              unsigned char result;
              int ix;
          
              for(ix=0;ix<127;ix++)
              {
                  result = (data ^ ((data & 0x40) << 1)) & 0x80;
          
                  if ( result & 0x80 )
                  {
                      putchar('1');
                  }
                  else
                  {
                      putchar('0');
                  }
          
                  data = (data >> 1) | result;
              }
          
              putchar('\n');
          
              return 0;
          }
          But the results are not very random

          011011011011011 011011011011011 011011011011011 011011011011011 011011011011011 011011011011011 011011011011011 011011011011011 0110110

          As you can see.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by D_C
            Ignoring the spaces, the result is: 000100110101111 repeated eight and a half times. I think chaotic is a better word than random.
            Actually chaotic is a really poor word for it,a chaotic system never repeats which you admit yours does.

            Still trying to work out what your code actualy does though :D

            Comment

            Working...