Velocity Reviews > Random floats function

# Random floats function

Michel Rouzic
Guest
Posts: n/a

 10-12-2005
I tried making a function that would fill half of an array with random
doubles that would go from -pi to +pi. Unfortunatly, all it ever
returns is -pi. I haven't used an already existing random number
generating function because I wasn't sure to know how to use them so I
created my own based on the random number generator from the INMOS
Transputer Development system which is x[n+1] = (1664525 x[n]) mod 2^32

double modfloat(double a, double b)
{
double c, d, x;

c=a/b;
d=(int32_t) c;
x=c-d;
return x;
}

double dblrand(double x)
{
return modfloat((1664525 * x * pow(2, 32)), pow(2, 32)) / pow(2, 32);
}

void phasenoise(double *s, int32_t M)
{
int32_t i;
time_t seed=time(NULL);
double pi=atan(1.0)*4.0;

s[M/2+1]=dblrand((double) seed/pow(2, 32)) * (2*pi) - pi;
for (i=M/2+2; i<M; i++)
s[i]=dblrand((s[i-1]+pi) / (2*pi)) * (2*pi) - pi;
}

Michael Mair
Guest
Posts: n/a

 10-12-2005
Michel Rouzic wrote:
> I tried making a function that would fill half of an array with random
> doubles that would go from -pi to +pi. Unfortunatly, all it ever
> returns is -pi. I haven't used an already existing random number
> generating function because I wasn't sure to know how to use them so I
> created my own based on the random number generator from the INMOS
> Transputer Development system which is x[n+1] = (1664525 x[n]) mod 2^32
>
> double modfloat(double a, double b)
> {
> double c, d, x;
>
> c=a/b;
> d=(int32_t) c;

uint32_t, if anything. However, this is not guaranteed to
work either.

> x=c-d;
> return x;
> }

BTW:
You do need to invent modfloat(), use fmod() instead.

> double dblrand(double x)
> {
> return modfloat((1664525 * x * pow(2, 32)), pow(2, 32)) / pow(2, 32);

^^^^^^^^^^
Lose that one.
a*b%b == 0
So, you always get back something near 0.0, which leads with
> }
>
> void phasenoise(double *s, int32_t M)
> {
> int32_t i;
> time_t seed=time(NULL);
> double pi=atan(1.0)*4.0;
>
> s[M/2+1]=dblrand((double) seed/pow(2, 32)) * (2*pi) - pi;
> for (i=M/2+2; i<M; i++)
> s[i]=dblrand((s[i-1]+pi) / (2*pi)) * (2*pi) - pi;
> }
>

Notes:
If the number of random digits does not play that much of a role,
then use (double)rand()/RANDMAX instead (with an appropriate seed
set by srand()).
Otherwise, use another off-the-shelf generator -- it probably has
better mathematical properties than your generator. This is not
the right place to discuss this, though.

I would rather work with symbolic constants instead of
pow(2, 32) and pi.
A convenient portable way for the former is
#define POW2TO32_DBL ((1UL << 31)*2.0)
For the latter, just take enough digits of pi.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.

Michel Rouzic
Guest
Posts: n/a

 10-12-2005

Michael Mair wrote:
> Michel Rouzic wrote:
> > I tried making a function that would fill half of an array with random
> > doubles that would go from -pi to +pi. Unfortunatly, all it ever
> > returns is -pi. I haven't used an already existing random number
> > generating function because I wasn't sure to know how to use them so I
> > created my own based on the random number generator from the INMOS
> > Transputer Development system which is x[n+1] = (1664525 x[n]) mod 2^32
> >
> > double modfloat(double a, double b)
> > {
> > double c, d, x;
> >
> > c=a/b;
> > d=(int32_t) c;

>
> uint32_t, if anything. However, this is not guaranteed to
> work either.
>
> > x=c-d;
> > return x;
> > }

>
> BTW:
> You do need to invent modfloat(), use fmod() instead.

> > double dblrand(double x)
> > {
> > return modfloat((1664525 * x * pow(2, 32)), pow(2, 32)) / pow(2, 32);

> ^^^^^^^^^^
> Lose that one.
> a*b%b == 0
> So, you always get back something near 0.0, which leads with

yeah but no, x is a double between 0 and 1, if i do that it's to get
back to the previous result before it's divided.

> > }
> >
> > void phasenoise(double *s, int32_t M)
> > {
> > int32_t i;
> > time_t seed=time(NULL);
> > double pi=atan(1.0)*4.0;
> >
> > s[M/2+1]=dblrand((double) seed/pow(2, 32)) * (2*pi) - pi;
> > for (i=M/2+2; i<M; i++)
> > s[i]=dblrand((s[i-1]+pi) / (2*pi)) * (2*pi) - pi;
> > }
> >

>
> Notes:
> If the number of random digits does not play that much of a role,
> then use (double)rand()/RANDMAX instead (with an appropriate seed
> set by srand()).

ok, so how do I do that (that's where I sound like a newbie), i do
something like srand(time(NULL)); and then (double) rand()/RANDMAX??
And I admit it kinda annoys me to use a random generator with only 2^15
possibilities (right?) but I guess it should be alright.

> Otherwise, use another off-the-shelf generator -- it probably has
> better mathematical properties than your generator. This is not
> the right place to discuss this, though.
>
> I would rather work with symbolic constants instead of
> pow(2, 32) and pi.
> A convenient portable way for the former is
> #define POW2TO32_DBL ((1UL << 31)*2.0)

I have no idea what ((1UL << 31)*2.0) can mean..

> For the latter, just take enough digits of pi.

What's wrong with the way I do my pi?

Michel Rouzic
Guest
Posts: n/a

 10-12-2005

Michael Mair wrote:
> Michel Rouzic wrote:
> > I tried making a function that would fill half of an array with random
> > doubles that would go from -pi to +pi. Unfortunatly, all it ever
> > returns is -pi. I haven't used an already existing random number
> > generating function because I wasn't sure to know how to use them so I
> > created my own based on the random number generator from the INMOS
> > Transputer Development system which is x[n+1] = (1664525 x[n]) mod 2^32
> >
> > double modfloat(double a, double b)
> > {
> > double c, d, x;
> >
> > c=a/b;
> > d=(int32_t) c;

>
> uint32_t, if anything. However, this is not guaranteed to
> work either.
>
> > x=c-d;
> > return x;
> > }

>
> BTW:
> You do need to invent modfloat(), use fmod() instead.
>
> > double dblrand(double x)
> > {
> > return modfloat((1664525 * x * pow(2, 32)), pow(2, 32)) / pow(2, 32);

> ^^^^^^^^^^
> Lose that one.
> a*b%b == 0
> So, you always get back something near 0.0, which leads with
> > }
> >
> > void phasenoise(double *s, int32_t M)
> > {
> > int32_t i;
> > time_t seed=time(NULL);
> > double pi=atan(1.0)*4.0;
> >
> > s[M/2+1]=dblrand((double) seed/pow(2, 32)) * (2*pi) - pi;
> > for (i=M/2+2; i<M; i++)
> > s[i]=dblrand((s[i-1]+pi) / (2*pi)) * (2*pi) - pi;
> > }
> >

>
> Notes:
> If the number of random digits does not play that much of a role,
> then use (double)rand()/RANDMAX instead (with an appropriate seed
> set by srand()).
> Otherwise, use another off-the-shelf generator -- it probably has
> better mathematical properties than your generator. This is not
> the right place to discuss this, though.
>
> I would rather work with symbolic constants instead of
> pow(2, 32) and pi.
> A convenient portable way for the former is
> #define POW2TO32_DBL ((1UL << 31)*2.0)
> For the latter, just take enough digits of pi.
>
>
> Cheers
> Michael
> --
> E-Mail: Mine is an /at/ gmx /dot/ de address.

I used fmod() instead of my modfloat() and now it works. Well, now the
values seem to be quite random and in the right range, so I guess I'll
keep using my dblrand() function. Thanks alot for help!

Michael Mair
Guest
Posts: n/a

 10-12-2005
Michel Rouzic wrote:
> Michael Mair wrote:
>
>>Michel Rouzic wrote:

<snip!>
>>
>>>double dblrand(double x)
>>>{
>>> return modfloat((1664525 * x * pow(2, 32)), pow(2, 32)) / pow(2, 32);

>> ^^^^^^^^^^
>>Lose that one.
>> a*b%b == 0
>>So, you always get back something near 0.0, which leads with

>
> yeah but no, x is a double between 0 and 1, if i do that it's to get
> back to the previous result before it's divided.

You are right, I did not really think about that one...
Question: Why are you not just using
return fmod(1664525.0 * x, 1.0);

>>Notes:
>>If the number of random digits does not play that much of a role,
>>then use (double)rand()/RANDMAX instead (with an appropriate seed
>>set by srand()).

>
> ok, so how do I do that (that's where I sound like a newbie), i do
> something like srand(time(NULL)); and then (double) rand()/RANDMAX??
> And I admit it kinda annoys me to use a random generator with only 2^15
> possibilities (right?) but I guess it should be alright.

RANDMAX is _at_least_ 2^15-1 -- your implementation's standard
library is allowed to do better...

>>I would rather work with symbolic constants instead of
>>pow(2, 32) and pi.
>>A convenient portable way for the former is
>>#define POW2TO32_DBL ((1UL << 31)*2.0)

>
> I have no idea what ((1UL << 31)*2.0) can mean..

Okay, a << N is a*2^N and has the same type as a. As int is
only guaranteed to have at least 16 bits, I use unsigned long.
1UL is a constant of type unsigned long with value 1.
Unfortunately, unsigned long is only guaranteed to have 32 bits
(and can hold 2^31 but not necessarily 2^32).
As we need a constant of type double, I just multiply the best
I can do by an appropriate double factor.
2^31 as double consequently is (1UL<<31)*1.0, as float
(1UL<<31)*1.0F and so on.
I prefer
((1UL << 31)*2.0)
to actually writing
4294967296.0
as it is easier to see that the former is a power of 2.

So, why bother at all with symbolic constants?
My solution gives you a compile time constant whereas
your solution gives you something which may be computed each
and every time. In addition, you can emphasize the _role_
of the constant to tell one nameless 2^32 from another which
comes in handy when you have to change it...

>>For the latter, just take enough digits of pi.

>
> What's wrong with the way I do my pi?

1) The C standard makes no guarantee whatsoever about the
precision of the math functions from the library. So, "your"
pi could be way off.
2) Your pi has to be recomputed at every function call.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.

Michel Rouzic
Guest
Posts: n/a

 10-13-2005

Michael Mair wrote:
> Michel Rouzic wrote:
> > Michael Mair wrote:
> >
> >>Michel Rouzic wrote:

> <snip!>
> >>
> >>>double dblrand(double x)
> >>>{
> >>> return modfloat((1664525 * x * pow(2, 32)), pow(2, 32)) / pow(2, 32);
> >> ^^^^^^^^^^
> >>Lose that one.
> >> a*b%b == 0
> >>So, you always get back something near 0.0, which leads with

> >
> > yeah but no, x is a double between 0 and 1, if i do that it's to get
> > back to the previous result before it's divided.

>
> You are right, I did not really think about that one...
> Question: Why are you not just using
> return fmod(1664525.0 * x, 1.0);

oh yeah of course, it's much simpler.

> >>Notes:
> >>If the number of random digits does not play that much of a role,
> >>then use (double)rand()/RANDMAX instead (with an appropriate seed
> >>set by srand()).

> >
> > ok, so how do I do that (that's where I sound like a newbie), i do
> > something like srand(time(NULL)); and then (double) rand()/RANDMAX??
> > And I admit it kinda annoys me to use a random generator with only 2^15
> > possibilities (right?) but I guess it should be alright.

>
> RANDMAX is _at_least_ 2^15-1 -- your implementation's standard
> library is allowed to do better...
>
> >>I would rather work with symbolic constants instead of
> >>pow(2, 32) and pi.
> >>A convenient portable way for the former is
> >>#define POW2TO32_DBL ((1UL << 31)*2.0)

> >
> > I have no idea what ((1UL << 31)*2.0) can mean..

>
> Okay, a << N is a*2^N and has the same type as a. As int is
> only guaranteed to have at least 16 bits, I use unsigned long.
> 1UL is a constant of type unsigned long with value 1.
> Unfortunately, unsigned long is only guaranteed to have 32 bits
> (and can hold 2^31 but not necessarily 2^32).
> As we need a constant of type double, I just multiply the best
> I can do by an appropriate double factor.
> 2^31 as double consequently is (1UL<<31)*1.0, as float
> (1UL<<31)*1.0F and so on.
> I prefer
> ((1UL << 31)*2.0)
> to actually writing
> 4294967296.0
> as it is easier to see that the former is a power of 2.
>
> So, why bother at all with symbolic constants?
> My solution gives you a compile time constant whereas
> your solution gives you something which may be computed each
> and every time. In addition, you can emphasize the _role_
> of the constant to tell one nameless 2^32 from another which
> comes in handy when you have to change it...

OK, so it's not going to compute it everytime?

> >>For the latter, just take enough digits of pi.

> >
> > What's wrong with the way I do my pi?

>
> 1) The C standard makes no guarantee whatsoever about the
> precision of the math functions from the library. So, "your"
> pi could be way off.
> 2) Your pi has to be recomputed at every function call.

oh, ok, well, I guess it's gonna be better if I stop doing that and put

Keith Thompson
Guest
Posts: n/a

 10-13-2005
"Michel Rouzic" <(E-Mail Removed)> writes:
> Michael Mair wrote:
>> Michel Rouzic wrote:

[...]
>> > What's wrong with the way I do my pi?

>>
>> 1) The C standard makes no guarantee whatsoever about the
>> precision of the math functions from the library. So, "your"
>> pi could be way off.
>> 2) Your pi has to be recomputed at every function call.

>
> oh, ok, well, I guess it's gonna be better if I stop doing that and put

That's 17 significant digits; long double often has more precision
than that.

Using 40 decimal digits will cover anything with a mantissa up to 128
bits, which is enough for any hardware I've ever used. Use an 'L'
suffix to make sure you get the full precision.

It's very likely that you can get away with far less precision,
depending on the application, but using more digits than you'll ever
need isn't a burden, and it means one less thing to worry about if

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

pete
Guest
Posts: n/a

 10-13-2005
Keith Thompson wrote:
>
> "Michel Rouzic" <(E-Mail Removed)> writes:
> > Michael Mair wrote:
> >> Michel Rouzic wrote:

> [...]
> >> > What's wrong with the way I do my pi?
> >>
> >> 1) The C standard makes no guarantee whatsoever about the
> >> precision of the math functions from the library. So, "your"
> >> pi could be way off.
> >> 2) Your pi has to be recomputed at every function call.

> >
> > oh, ok, well, I guess it's gonna be better
> > if I stop doing that and put
> > #define pi 3.1415926535897932 instead

>
> That's 17 significant digits; long double often has more precision
> than that.
>
> Using 40 decimal digits will cover anything with a mantissa up to 128
> bits, which is enough for any hardware I've ever used. Use an 'L'
> suffix to make sure you get the full precision.
>
> It's very likely that you can get away with far less precision,
> depending on the application, but using more digits than you'll ever
> need isn't a burden, and it means one less thing to worry about if

I got all the precision of a double right here:

/* BEGIN pi.c */

#include <stdio.h>
#include <float.h>
#include <math.h>

double pi(void);
double pi2(void);

int main(void)
{
static double Pi, Pi2;

Pi = pi();
Pi2 = pi2();

printf("Pi is %f\n", Pi);
printf("Pi2 is %f\n", Pi2);
printf("Pi - 4 * atan(1) is %e\n", Pi - 4 * atan(1));
printf("Pi2 - 4 * atan(1) is %e\n", Pi2 - 4 * atan(1));
printf("Pi - Pi2 is %e\n", Pi - Pi2);
return 0;
}

double pi(void)
{
double pi, b, first, second, numerator;
unsigned denominator;

pi = 0;
numerator = 1 / 5.0;
denominator = 1;
do {
first = numerator / denominator;
denominator += 2;
numerator /= 25.0;
second = numerator / denominator;
denominator += 2;
numerator /= 25.0;
b = first - second;
pi += b;
} while (b > DBL_EPSILON);
pi *= 4;
numerator = 1 / 239.0;
denominator = 1;
do {
first = numerator / denominator;
denominator += 2;
numerator /= 57121.0;
second = numerator / denominator;
denominator += 2;
numerator /= 57121.0;
b = first - second;
pi -= b;
} while (b > DBL_EPSILON);
return 4 * pi;
}

double pi2(void)
{
double pi, b, first, second, numerator;
unsigned denominator;

pi = 0;
numerator = 1 / 2.0;
denominator = 1;
do {
first = numerator / denominator;
denominator += 2;
numerator /= 4.0;
second = numerator / denominator;
denominator += 2;
numerator /= 4.0;
b = first - second;
pi += b;
} while (b > DBL_EPSILON);
numerator = 1 / 3.0;
denominator = 1;
do {
first = numerator / denominator;
denominator += 2;
numerator /= 9.0;
second = numerator / denominator;
denominator += 2;
numerator /= 9.0;
b = first - second;
pi += b;
} while (b > DBL_EPSILON);
return 4 * pi;
}

/* END pi.c */

--
pete

pete
Guest
Posts: n/a

 10-13-2005
pete wrote:
>

Based on the magnitude of the value of the pi variable
when the loops complete, I think better epsilon numbers would be:

> double pi(void)
> {
> double pi, b, first, second, numerator;
> unsigned denominator;
>
> pi = 0;
> numerator = 1 / 5.0;
> denominator = 1;
> do {
> first = numerator / denominator;
> denominator += 2;
> numerator /= 25.0;
> second = numerator / denominator;
> denominator += 2;
> numerator /= 25.0;
> b = first - second;
> pi += b;
> } while (b > DBL_EPSILON);

} while (b > 0.125 * DBL_EPSILON);

> pi *= 4;
> numerator = 1 / 239.0;
> denominator = 1;
> do {
> first = numerator / denominator;
> denominator += 2;
> numerator /= 57121.0;
> second = numerator / denominator;
> denominator += 2;
> numerator /= 57121.0;
> b = first - second;
> pi -= b;
> } while (b > DBL_EPSILON);

} while (b > 0.5 * DBL_EPSILON);

> return 4 * pi;
> }
>
> double pi2(void)
> {
> double pi, b, first, second, numerator;
> unsigned denominator;
>
> pi = 0;
> numerator = 1 / 2.0;
> denominator = 1;
> do {
> first = numerator / denominator;
> denominator += 2;
> numerator /= 4.0;
> second = numerator / denominator;
> denominator += 2;
> numerator /= 4.0;
> b = first - second;
> pi += b;
> } while (b > DBL_EPSILON);

} while (b > 0.25 * DBL_EPSILON);

> numerator = 1 / 3.0;
> denominator = 1;
> do {
> first = numerator / denominator;
> denominator += 2;
> numerator /= 9.0;
> second = numerator / denominator;
> denominator += 2;
> numerator /= 9.0;
> b = first - second;
> pi += b;
> } while (b > DBL_EPSILON);

} while (b > 0.5 * DBL_EPSILON);

> return 4 * pi;
> }
>
> /* END pi.c */

--
pete

pete
Guest
Posts: n/a

 10-13-2005
pete wrote:
>
> Keith Thompson wrote:
> >
> > "Michel Rouzic" <(E-Mail Removed)> writes:
> > > Michael Mair wrote:
> > >> Michel Rouzic wrote:

> > [...]
> > >> > What's wrong with the way I do my pi?
> > >>
> > >> 1) The C standard makes no guarantee whatsoever about the
> > >> precision of the math functions from the library. So, "your"
> > >> pi could be way off.
> > >> 2) Your pi has to be recomputed at every function call.
> > >
> > > oh, ok, well, I guess it's gonna be better
> > > if I stop doing that and put
> > > #define pi 3.1415926535897932 instead

> >
> > That's 17 significant digits; long double often has more precision
> > than that.
> >
> > Using 40 decimal digits will cover
> > anything with a mantissa up to 128
> > bits, which is enough for any hardware I've ever used. Use an 'L'
> > suffix to make sure you get the full precision.
> >
> > It's very likely that you can get away with far less precision,
> > depending on the application, but using more digits than you'll ever
> > need isn't a burden, and it means one less thing to worry about if

>
> I got all the precision of a double right here:
>
> /* BEGIN pi.c */

Subsequent testing indicates maybe I don't
got all the precision of a double.

--
pete