Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > random number generator

Reply
Thread Tools

random number generator

 
 
asdf
Guest
Posts: n/a
 
      10-21-2006
I want a random number generator, the random number should be subject a
uniform distribution in [0,1]. Could you please give me some hints?
Thanks.

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      10-21-2006
"asdf" <(E-Mail Removed)> wrote:

> I want a random number generator, the random number should be subject a
> uniform distribution in [0,1]. Could you please give me some hints?
> Thanks.


http://members.cox.net/srice1/random/crandom.html

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
 
Reply With Quote
 
 
 
 
Peter Jansson
Guest
Posts: n/a
 
      10-21-2006
asdf wrote:
> I want a random number generator, the random number should be subject a
> uniform distribution in [0,1]. Could you please give me some hints?
> Thanks.
>


You might be interested in the Mersenne Twister:
http://www.math.sci.hiroshima-u.ac.j...at/MT/emt.html

Which have a link to:
http://www.math.sci.hiroshima-u.ac.j...ES/mt19937ar.c


Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      10-22-2006
asdf wrote:
> I want a random number generator, the random number should be subject a
> uniform distribution in [0,1]. Could you please give me some hints?
> Thanks.
>


If you've got a copy of TR1, there are random number generators galore
in it. Dinkumware implements all of the TR. Boost has, among other
pieces, the random number generators (although I haven't checked to see
if they're up to date with the changes made for TR1).

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
Reply With Quote
 
noone
Guest
Posts: n/a
 
      10-23-2006
On Sat, 21 Oct 2006 07:53:59 -0700, asdf wrote:

> I want a random number generator, the random number should be subject a
> uniform distribution in [0,1]. Could you please give me some hints?
> Thanks.


Are you sure you want (1) included in your range?

Others have given you pointers to RNG literature. One thing I've noticed
that seems to be an almost universal misuse of random number functions
such as the srand() and rand() functions is that programmers who
use them to generate numbers [0..1) virtually NEVER check to see if the
returned value of rand()==RAND_MAX. While the probability of this
happening is 1/RAND_MAX, you should check because rand()/RAND_MAX could
end up outside of your intended range.


For simple non-cryptographic uniform distributions the rand() function is
often adequate but for more stringent requirements the BOOST library
provides several RNGs. Of course as with many open source projects, the
documentation is lacking...at least it was when I last used them.


 
Reply With Quote
 
Steve Pope
Guest
Posts: n/a
 
      10-23-2006
noone <(E-Mail Removed)> wrote:

> One thing I've noticed that seems to be an almost universal
> misuse of random number functions such as the srand() and rand()
> functions is that programmers who use them to generate numbers
> [0..1) virtually NEVER check to see if the returned value of
> rand()==RAND_MAX. While the probability of this happening is
> 1/RAND_MAX, you should check because rand()/RAND_MAX could end
> up outside of your intended range.


I usually do something like

int x = rand() & 0x3fffffff;
double y = (double) x / (double) 0x40000000;

y is now uniform in [0,1).

A test for RAND_MAX >= 0x3fffffff would be nice as you say.
On my system RAND_MAX is pow(2,31)-1 which is what you'd expect,
so it works.

Steve
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      10-23-2006
noone wrote:
> On Sat, 21 Oct 2006 07:53:59 -0700, asdf wrote:
>
>> I want a random number generator, the random number should be subject a
>> uniform distribution in [0,1]. Could you please give me some hints?
>> Thanks.

>
> Are you sure you want (1) included in your range?
>
> Others have given you pointers to RNG literature. One thing I've noticed
> that seems to be an almost universal misuse of random number functions
> such as the srand() and rand() functions is that programmers who
> use them to generate numbers [0..1) virtually NEVER check to see if the
> returned value of rand()==RAND_MAX. While the probability of this
> happening is 1/RAND_MAX, you should check because rand()/RAND_MAX could
> end up outside of your intended range.
>


Better yet, don't check, but map the range so that this isn't a problem:

rand() / ((double)RAND_MAX + 1)

>
> For simple non-cryptographic uniform distributions the rand() function is
> often adequate but for more stringent requirements the BOOST library
> provides several RNGs. Of course as with many open source projects, the
> documentation is lacking...at least it was when I last used them.
>
>


There's documentation for the TR1 random number generators in chapter 13
of my book, "The Standard C++ Library Extensions." The Boost generators
are close to what's in TR1.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
Reply With Quote
 
Martin Steen
Guest
Posts: n/a
 
      10-23-2006
asdf wrote:
> I want a random number generator, the random number should be subject a
> uniform distribution in [0,1]. Could you please give me some hints?
> Thanks.
>


double r = (double) rand() / RAND_MAX;

http://www.cplusplus.com/ref/cstdlib/rand.html

-Martin




 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      10-23-2006
Pete Becker wrote:
> noone wrote:
>> On Sat, 21 Oct 2006 07:53:59 -0700, asdf wrote:
>>
>>> I want a random number generator, the random number should be subject a
>>> uniform distribution in [0,1]. Could you please give me some hints?
>>> Thanks.

>>
>> Are you sure you want (1) included in your range?
>>
>> Others have given you pointers to RNG literature. One thing I've noticed
>> that seems to be an almost universal misuse of random number functions
>> such as the srand() and rand() functions is that programmers who
>> use them to generate numbers [0..1) virtually NEVER check to see if the
>> returned value of rand()==RAND_MAX. While the probability of this
>> happening is 1/RAND_MAX, you should check because rand()/RAND_MAX could
>> end up outside of your intended range.
>>

>
> Better yet, don't check, but map the range so that this isn't a problem:
>
> rand() / ((double)RAND_MAX + 1)
>


Just a clarification (since I confused myself): this gives a uniform
distribution over the half-open range [0.0, 1.0), as the message from
"noone" suggests. It does not answer the original question of how to get
a uniform distribution over the closed range [0.0, 1.0].

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Random Number Generator?? Kingsley Oteng VHDL 11 08-09-2010 10:00 AM
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Random number generator, generating 10 different numbers. Need Help. Wally ASP .Net 1 03-20-2006 12:19 AM
Random number/letter generator tshad ASP .Net 3 05-06-2005 06:15 PM



Advertisments