Skip to content

Instantly share code, notes, and snippets.

@tomkaith13
Created May 30, 2014 06:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomkaith13/4ef3bbf919eafbf0fa43 to your computer and use it in GitHub Desktop.
Save tomkaith13/4ef3bbf919eafbf0fa43 to your computer and use it in GitHub Desktop.
Linear Feedback Shift Register Based uniform random number generator ... it generates 2**32 -1 random numbers
#include<iostream>
using namespace std;
int rand_gen(unsigned int x) {
/*
XOR Linear feedback shift register based random gen
*/
unsigned int a=1;
unsigned int c=0,d=0,e=0;
c = x&a;
d = (x>>1) & a;
x >>= 1;
x |= ((c^d)<<31);
return x;
}
int main() {
unsigned int x=1;
int i;
for (i=0;i<135;i++)
{
x = rand_gen(x);
cout<<"random="<<x<<endl;
}
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment