Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //32-bit xorshift lfsr
- int lfsr32(){
- static int last=0xfeedbac;//k (starting number must be non-zero)
- int current=last ^ (last<<13);
- current^=last>>17;
- current^=last<< 5;
- last=current;
- return last;
- }
- //should return a float between 0.0 -> 1.0
- float lfsr_float(){
- union { int i; float f; } value;
- //basically, all bits are random, except the exponent bits always make 2^0
- //(this should = -1.99...->-1.0 OR 1.0->1.99...)
- value.i=(lfsr32()&0x807fffff) | 0x3f800000;
- //1.0 if original value.f = -1.0 (as i don't think 1.0 could occur otherwise)
- if(value.i==0xbf800000) return 1.0f; //1.0
- else if(value.f>=0.0f) return (value.f-1)*0.5f; //0.0 -> 0.499...
- else /*if(value.f<0.0f)*/ return 0.5f-(value.f+1)*0.5f; //5.0 -> 0.99...
- }
- //should do the same thing with a larger range, but uses a division
- static inline float lfsr_float2(){
- return (float)((unsigned)lfsr32())/0xffffffff;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement