-3

Really, i want to get random bit sequence pattern, please tell me logic how to get sequence pattern.

I tried with below (example) code for getting Random bit sequence pattern like this

2147483663, 1073741831, 536870915, 2415919105, 1207959552, 2751463424, 3523215360, 1761607680, 3028287488, 3661627392, 1830813696

But i am getting only constant value, without any change like this

1932117999, 1932117999, 1932117999, 1932117999, 1932117999, 1932117999, 1932117999, 1932117999, 1932117999, 1932117999

Please find the attachment and suggest me. I am grateful.

int main()
{
  unsigned int x=12131222, y=898565;
  unsigned int z=58964796, c=6543217;
  unsigned long long t, result;
  unsigned int r;
  int i;

  x = 14900243 * x+ 123456789;
  y ^= y << 21;
  y ^= y >> 17;
  y ^= y << 30;

  /* Do not set y=0! */

  t = 42945843 * z + c;
  c = t >> 32;
  z = t;

  result=(unsigned int)(x>>32) + (unsigned int)y +z;

  for ( i = 0; i < 10; i++) 
  {
    r = result; //use the LFSR
    printf ("%u, ", r);
  }

  return 0;
}
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
Thaus
  • 23
  • 5
  • 2
    Your loop does not modify `result` but just prints it 10 times. How should the value change? You need to perform your operations each time you execute the loop. – Gerhardh May 19 '17 at 09:19
  • I suspect `(unsigned int)(x>>32) + (unsigned int)y +z;` needs to be done using wider math and with `<<`. `result=(1ULL*x)<<32) + y +z;` – chux - Reinstate Monica May 19 '17 at 09:20
  • `t = 42945843 * z + c;` only assigned `t` a value in the `unsigned` range. Again, wider math needed. What is the source or your LFSR algorithm? – chux - Reinstate Monica May 19 '17 at 09:27
  • Can i use register concept for random bit sequence pattern. Actually i need to implement this program in hardware. Source Code ` void Polynomial_lfsr(int &dout) { static int reg; int pattern; int load_data =0xACF5; int i=0; reg = load_data; for (i=0;i<=32;i++) { pattern= ((reg >> 0) ^ (reg >> 2) ^ (reg >> 3) ^ (reg >> 5) ) & 1; reg = (reg >> 1) | (bit << 15); reg[i] = pattern; // assigns the feedback polynomial result to bit zero of the LFSR dout = reg; } }` How can i insert sequence pattern into above code. – Thaus May 19 '17 at 09:42

2 Answers2

0

Firstly, create special function for it. Secondly, use static variable for save progress at each iteration.

#include <stdio.h>

unsigned long long random()
{
        static unsigned int x =12131222, y=898565;
        static unsigned int z=58964796, c=6543217;
        unsigned long long t, result;
        x = 14900243 * x+ 123456789;
        y ^= y << 21;
        y ^= y >> 17;
        y ^= y << 30;
        /* Do not set y=0! */
        t = 42945843 * z + c;
        c = t >> 32;
        z = t;
        return result=(unsigned int)(x>>32) + (unsigned int)y +z;
}
int main()
{
        unsigned int r;
        int i;
        for ( i = 0; i < 10; i++)
        {
                r = random(); //use the LFSR
                printf ("%u, ", r);
        }
        return 0;
}

Or move your code inside loop:

#include <stdio.h>

int main()
{
        unsigned int x =12131222, y=898565;
        unsigned int z=58964796, c=6543217;
        unsigned long long t, result;

        unsigned int r;
        int i;
        for ( i = 0; i < 10; i++)
        {
                x = 14900243 * x+ 123456789;
                y ^= y << 21;
                y ^= y >> 17;
                y ^= y << 30;
                /* Do not set y=0! */
                t = 42945843 * z + c;
                c = t >> 32;
                z = t;
                result=(unsigned int)(x>>32) + (unsigned int)y +z;
                r = result; //use the LFSR
                printf ("%u, ", r);
        }
        return 0;
}
knst
  • 523
  • 2
  • 16
  • Can i use register concept for random bit sequence pattern. Actually i need to implement this program in hardware. Source Code ` void Polynomial_lfsr(int &dout) { static int reg; int pattern; int load_data =0xACF5; int i=0; reg = load_data; for (i=0;i<=32;i++) { pattern= ((reg >> 0) ^ (reg >> 2) ^ (reg >> 3) ^ (reg >> 5) ) & 1; reg = (reg >> 1) | (bit << 15); reg[i] = pattern; // assigns the feedback polynomial result to bit zero of the LFSR dout = reg; } }` How can i insert sequence pattern into above code – Thaus May 19 '17 at 09:47
  • Update question, please. Very difficult to understand code without formatting – knst May 19 '17 at 09:50
0

Usually, you should not write your own pseudo random number code. Use srand() for seeding and rand() for computation as shown for example here: C++ random float number generation There are still options with better quality random numbers, but this is a starting point.

Community
  • 1
  • 1
Gerriet
  • 1,302
  • 14
  • 20