473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

generating random numbers with Uniform(a,b)?

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
8 25404
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: drs | last post by:
Is there any way to generate random numbers based on arbitrary real valued functions? I am looking for something like random.gauss() but with natural log and exponential functions. thanks, -d
4
by: mescaline | last post by:
hi, i'm new to C++ could anyone refer me to a good site / good examples of random numbers? in particular including: 1) the commnds to obtain normally and exponenetially distributed r...
24
by: Stavros Christoforou | last post by:
Hello everyone, I was wondering if someone could help me with an issue I have in C++. I want to select random points within the volume of a sphere. I know how to get random numbers using srand()...
21
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to...
4
by: Dimos | last post by:
Hello All, I need some help with random number generation. What I need exactly is: To create a few thousand numbers, decimal and integers, between 5 and 90, and then to export them as a...
14
by: Anthony Liu | last post by:
I am at my wit's end. I want to generate a certain number of random numbers. This is easy, I can repeatedly do uniform(0, 1) for example. But, I want the random numbers just generated sum up...
22
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
1
by: johntilster | last post by:
How do I get uniform random numbers in the log domain? I need a random number in the log domain between -infinity and -0.69314718, which is equivalent to a range from 0 to 0.5. I usually use...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.