Skip to content

Instantly share code, notes, and snippets.

@pathologicalhandwaving
Forked from tomkaith13/random_LSFR.cpp
Created October 17, 2015 03:05
Show Gist options
  • Save pathologicalhandwaving/3a6db832ca69c7a0a281 to your computer and use it in GitHub Desktop.
Save pathologicalhandwaving/3a6db832ca69c7a0a281 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