From: Vitaly V. B. <vit...@us...> - 2004-10-03 17:00:48
|
On Sun, 03 Oct 2004 18:11:35 +0200 Dennis Smit <sy...@yo...> wrote: > On Sun, 2004-10-03 at 18:48 +0300, Vitaly V. Bursov wrote: > > > I've put some of the randomness request in CVS, not yet finished tho. > > when you convert unsigned int to float you loose entropy... > > I think it's ok to add double variant of functions... > > Go ahead :) OK :) few bits of code.... ========================== #include <stdlib.h> #include <stdio.h> #include <string.h> #define val_a 1664525L #define val_c 1013904223L #define RMAX 4294967295U static unsigned int _lv_randseed = 5415454; double rnd() { #if 0 union { unsigned int a[2]; double d; } val; unsigned int t; /* EXECUTION TIME * alu maths = 5 clocks * fsub = 4 clocks * TOTAL: 9 for double * WARNING: numbers should be ieee754 compliant */ t = _lv_randseed = val_a * _lv_randseed + val_c; val.a[0]=(t<<20); val.a[1]=0x3ff00000U | (t>>12); return (*(double*)&val.d)-1.0; #else unsigned int t; double x; /* EXECUTION TIME * fild - load int to fpu = 4 clocks * fdiv - normalize result = 16/20/24 clocks for signle/double/ext prec. * TOTAL: 24 for double */ t = _lv_randseed = val_a * _lv_randseed + val_c; x = (double)t/RMAX; return x; #endif } double x = 0.0; int main() { unsigned int i, t=1000000000; for (i=0;i<t;i++){ x += rnd(); } } ========================== the same idea can be used to generate floats. should I make a patch? -- Vitaly GPG Key ID: F95A23B9 |