By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,667 Members | 2,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,667 IT Pros & Developers. It's quick & easy.

generating random numbers with Uniform(a,b)?

P: n/a
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

Dec 12 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
In article <11**********************@73g2000cwn.googlegroups. com>,
<ki*********@gmail.comwrote:
>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.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Dec 12 '06 #2

P: n/a
ki*********@gmail.com wrote:
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[i] = 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.

--
Simon.
Dec 12 '06 #3

P: n/a
Simon Biber wrote:
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.

--
Thad
Dec 13 '06 #4

P: n/a
On Wed, 13 Dec 2006 00:54:38 -0700, in comp.lang.c , Thad Smith
<Th*******@acm.orgwrote:
>Simon Biber wrote:
>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.
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
Dec 13 '06 #5

P: n/a
In article <43********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.netwrote:
>>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.
>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Dec 13 '06 #6

P: n/a
Mark McIntyre wrote:
On Wed, 13 Dec 2006 00:54:38 -0700, in comp.lang.c , Thad Smith
<Th*******@acm.orgwrote:
>Simon Biber wrote:
>>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.

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.

--
Simon.
Dec 13 '06 #7

P: n/a
Mark McIntyre wrote:
<Th*******@acm.orgwrote:
>>Simon Biber wrote:
>>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.

Are you guys jumbling up mean, mode and median?
No, they're in their mean mode, and I am mediating. :-)

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 13 '06 #8

P: n/a
Thanks for your help. I will try applying this.

Thanks again,
Kay
Simon Biber wrote:
ki*********@gmail.com wrote:
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[i] = 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.

--
Simon.
Dec 13 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.