P: n/a

Hey all,
How do I generate random numbers with Uniform distribution Uniform(a,b)
using Cprogramming? 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  
Share this Question
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 Cprogramming? 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 pseudorandom numbers. Different pseudorandom generators have
different statistical properties.

I was very young in those days, but I was also rather dim.
 Christopher Priest  
P: n/a
 ki*********@gmail.com wrote:
Hey all,
How do I generate random numbers with Uniform distribution Uniform(a,b)
using Cprogramming? 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 straightforward 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 finergrained numbers, such as the possibility to generate
each individual floatingpoint 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 floatingpoint numbers are a
completely different distribution to uniformlydistributed real numbers.
The floatingpoint numbers are highly skewed because of the exponent
and mantissa format.
For example, generating uniformlydistributed floatingpoint numbers
from 1 to 1048576, you will find the mean is around 78500, not around
524000 as one might expect.

Simon.  
P: n/a

Simon Biber wrote:
For example, generating uniformlydistributed floatingpoint 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  
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 uniformlydistributed floatingpoint 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  
P: n/a

In article <43********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.netwrote:
>>For example, generating uniformlydistributed floatingpoint 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 floatingpoint 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.
Uniformlydistributed floatingpoint numbers are not usually useful
unless restricted to some range, and in that context I would expect
it to mean floatingpoint representations of uniformlydistributed
real numbers.
 Richard

"Consideration shall be given to the need for as many as 32 characters
in some alphabets"  X3.4, 1963.  
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 uniformlydistributed floatingpoint 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 equallyspaced within the
range.

Simon.  
P: n/a

Mark McIntyre wrote:
<Th*******@acm.orgwrote:
>>Simon Biber wrote:
>>For example, generating uniformlydistributed floatingpoint 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>  
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 Cprogramming? 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 straightforward 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 finergrained numbers, such as the possibility to generate
each individual floatingpoint 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 floatingpoint numbers are a
completely different distribution to uniformlydistributed real numbers.
The floatingpoint numbers are highly skewed because of the exponent
and mantissa format.
For example, generating uniformlydistributed floatingpoint numbers
from 1 to 1048576, you will find the mean is around 78500, not around
524000 as one might expect.

Simon.
  This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 21414
 replies: 8
 date asked: Dec 12 '06
