generating random numbers with Uniform(a,b)?

K

kiranchahar

Hey all,

How do I generate random numbers with Uniform distribution Uniform(a,b)
using C-programming? I want to generate uniform random numbers which
have mean following Uniform(p,q) and also variance as Uniform(s,t)? any
suggestion would be really appreciated.

Thanks,
Kay
 
W

Walter Roberson

How do I generate random numbers with Uniform distribution Uniform(a,b)
using C-programming? I want to generate uniform random numbers which
have mean following Uniform(p,q) and also variance as Uniform(s,t)? any
suggestion would be really appreciated.

Have you tried the FAQ? Have you tried searching the many previous
postings about random numbers?

There is, by the way, no way in standard C to generate random numbers:
only pseudo-random numbers. Different pseudo-random generators have
different statistical properties.
 
S

Simon Biber

Hey all,

How do I generate random numbers with Uniform distribution Uniform(a,b)
using C-programming? I want to generate uniform random numbers which
have mean following Uniform(p,q) and also variance as Uniform(s,t)? any
suggestion would be really appreciated.

The most straight-forward way is:

double uniform(double a, double b)
{
return rand() / (RAND_MAX + 1.0) * (b - a) + a;
}

This will return at most RAND_MAX different values equally spaced in the
range [a .. b).

If you need finer-grained numbers, such as the possibility to generate
each individual floating-point representation between the given values
with equal probability, you need a more sophisticated function.

The following function should achieve that, but it'll be slow if a and b
are close in magnitude.

double fp_uniform(double a, double b)
{
double t;
unsigned char *p = (unsigned char *)&t;
size_t i, n = 0;
do
{
for(i = 0; i < sizeof (double); i++)
{
p = rand() / (RAND_MAX + 1.0) * (UCHAR_MAX + 1.0);
}
n++;
}
while(t == 0 || isnan(t) || t < a || t > b);
return t;
}

Note of that uniformly distributed floating-point numbers are a
completely different distribution to uniformly-distributed real numbers.
The floating-point numbers are highly skewed because of the exponent
and mantissa format.

For example, generating uniformly-distributed floating-point numbers
from 1 to 1048576, you will find the mean is around 78500, not around
524000 as one might expect.
 
T

Thad Smith

Simon said:
For example, generating uniformly-distributed floating-point numbers
from 1 to 1048576, you will find the mean is around 78500, not around
524000 as one might expect.

I know what you mean, but if the mean is around 78500, then the
distribution isn't uniform across the interval, which is the normal
meaning of uniform distribution.
 
M

Mark McIntyre

I know what you mean, but if the mean is around 78500, then the
distribution isn't uniform across the interval, which is the normal
meaning of uniform distribution.

Are you guys jumbling up mean, mode and median?

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
S

Simon Biber

Mark said:
Are you guys jumbling up mean, mode and median?

No, we are both using 'mean' in the correct sense. The problem is that I
was misusing 'uniform' to mean something quite different to what it
usually does.

In a uniform distribution of integer numbers, one would expect an equal
probability of each number occurring. In a uniform distribution of real
numbers, one would expect an equal (infinitesimal) probability of each
number occurring.

If you arrange to generate a distribution of floating point numbers in a
given range such that there is an equal probability of each floating
point number in that range occurring, you will find that it is usually
nothing like a uniform distribution.

That's because when the range spans more than one exponent value, the
representable floating point numbers are not equally-spaced within the
range.
 
R

Richard Tobin

I know what you mean, but if the mean is around 78500, then the
distribution isn't uniform across the interval, which is the normal
meaning of uniform distribution.
[/QUOTE]
Are you guys jumbling up mean, mode and median?

No, he means if you choose floating point numbers such that each
distinct floating point number in that range is equally likely, the
mean will be around 78500. This is not surprising because there are
equally many floating point numbers in (for example) the intervals
[1,2) and [524288,1048576), so it will be biased towards smaller
values.

The median would be 1024 - there are as many floating-point numbers in
the range (1,1024) as in [1024,1048576) - and there is no useful mode
because there are equal numbers of each value.

All this assumes a binary floating point system, of course.

Uniformly-distributed floating-point numbers are not usually useful
unless restricted to some range, and in that context I would expect
it to mean floating-point representations of uniformly-distributed
real numbers.

-- Richard
 
K

kiranchahar

Thanks for your help. I will try applying this.

Thanks again,
Kay
Simon said:
Hey all,

How do I generate random numbers with Uniform distribution Uniform(a,b)
using C-programming? I want to generate uniform random numbers which
have mean following Uniform(p,q) and also variance as Uniform(s,t)? any
suggestion would be really appreciated.

The most straight-forward way is:

double uniform(double a, double b)
{
return rand() / (RAND_MAX + 1.0) * (b - a) + a;
}

This will return at most RAND_MAX different values equally spaced in the
range [a .. b).

If you need finer-grained numbers, such as the possibility to generate
each individual floating-point representation between the given values
with equal probability, you need a more sophisticated function.

The following function should achieve that, but it'll be slow if a and b
are close in magnitude.

double fp_uniform(double a, double b)
{
double t;
unsigned char *p = (unsigned char *)&t;
size_t i, n = 0;
do
{
for(i = 0; i < sizeof (double); i++)
{
p = rand() / (RAND_MAX + 1.0) * (UCHAR_MAX + 1.0);
}
n++;
}
while(t == 0 || isnan(t) || t < a || t > b);
return t;
}

Note of that uniformly distributed floating-point numbers are a
completely different distribution to uniformly-distributed real numbers.
The floating-point numbers are highly skewed because of the exponent
and mantissa format.

For example, generating uniformly-distributed floating-point numbers
from 1 to 1048576, you will find the mean is around 78500, not around
524000 as one might expect.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top