473,749 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Floating Point Random Number Generator

I know how to use rand() to generate random POSITIVE-INTEGER numbers.

But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0
with resolution of a double (i.e., every possible double value in the range
could come up with equal probability). I'd also like to be able to seed this
generator (e.g., via the clock) so that the same sequence of random values
don't come up every time.

Anybody have an easy and fast (computationall y) way to do this? Thanks in
advance...!
Nov 17 '05 #1
5 3351
Peteroid wrote:
I know how to use rand() to generate random POSITIVE-INTEGER numbers.

But, I'd like to generate a random DOUBLE number in the range of 0.0
to 1.0 with resolution of a double (i.e., every possible double value
in the range could come up with equal probability). I'd also like to
be able to seed this generator (e.g., via the clock) so that the same
sequence of random values don't come up every time.


A double in [0.0,1.0) has 2^52 distinct values. To generate such doubles,
first generate a random integer in [0,2^52) and then divide it by 2^52.
Note that all such integers, including 2^52 can be represented exactly as
doubles.

You might want to look at boost::random (see
http://www.boost.org/libs/random/index.html for details) for pseudo-random
generators that are good enough to generate 2^52 numbers without cycles.
You need to use a long period generator such as a Mersenne twister to really
get 2^52 values out of it. Your generator will have to generate 64 bit
values of course.

-cd
Nov 17 '05 #2
Thanks, Carl!

You gave me a better way than I was going. I'll create a class that allows
me to generate a floating-point number in the range of [0.0.,1.0] (yes,
closed on both sides) of arbitray bit-count resolution up to a limit (51, or
possibly even 63, sounds good, based on your response).

Say the bit-count is N. Generate an N-bit random number by generating N/16
number of random 16-bit integers (using rand()), and possibly 1 more for the
N%16 remaining bits (which is masked off the apprograte number of high bits
to get correct number of bytes), and then append them into a double by
shifting and adding. Then divide by a double version of (2^N - 1) and that
should (for some values of N) produce the closed interval random
floating-point number expressed as a double!

The seed is nor just srand()...

Thanx again...! :)

[==Peteroid==]

"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:uM******** ******@TK2MSFTN GP14.phx.gbl...
Peteroid wrote:
I know how to use rand() to generate random POSITIVE-INTEGER numbers.

But, I'd like to generate a random DOUBLE number in the range of 0.0
to 1.0 with resolution of a double (i.e., every possible double value
in the range could come up with equal probability). I'd also like to
be able to seed this generator (e.g., via the clock) so that the same
sequence of random values don't come up every time.
A double in [0.0,1.0) has 2^52 distinct values. To generate such doubles,
first generate a random integer in [0,2^52) and then divide it by 2^52.
Note that all such integers, including 2^52 can be represented exactly as
doubles.

You might want to look at boost::random (see
http://www.boost.org/libs/random/index.html for details) for pseudo-random
generators that are good enough to generate 2^52 numbers without cycles.
You need to use a long period generator such as a Mersenne twister to

really get 2^52 values out of it. Your generator will have to generate 64 bit
values of course.

-cd

Nov 17 '05 #3
Peteroid wrote:
Thanks, Carl!

You gave me a better way than I was going. I'll create a class that
allows me to generate a floating-point number in the range of
[0.0.,1.0] (yes, closed on both sides) of arbitray bit-count
resolution up to a limit (51, or possibly even 63, sounds good, based
on your response).

Say the bit-count is N. Generate an N-bit random number by generating
N/16 number of random 16-bit integers (using rand()), and possibly 1
more for the N%16 remaining bits (which is masked off the apprograte
number of high bits to get correct number of bytes), and then append
them into a double by shifting and adding. Then divide by a double
version of (2^N - 1) and that should (for some values of N) produce
the closed interval random floating-point number expressed as a
double!


Just be aware that the resulting numbers will very likely not cover all
possible values. The period of a 52 bit number generated from concatenation
of 4 values from a 16 bit rng followed by a mask to 52 bits will be at most
2^18 - nowhere near 2^52.

-cd
Nov 17 '05 #4
TOM
You may want to read the section on random number generators in
"Numerical Recipes in C". The text is free and on-line in PDF format by
permission of the publisher, at:

http://www.nr.com/

It's very difficult to build a good random number generator. The text
illustrates the problems and some ways around it, but is focused
on float rather than double.

-- Tom

"Peteroid" <pe************ @msn.com> wrote in message
news:u2******** ******@TK2MSFTN GP11.phx.gbl...
Thanks, Carl!

You gave me a better way than I was going. I'll create a class that allows
me to generate a floating-point number in the range of [0.0.,1.0] (yes,
closed on both sides) of arbitray bit-count resolution up to a limit (51,
or
possibly even 63, sounds good, based on your response).

Say the bit-count is N. Generate an N-bit random number by generating N/16
number of random 16-bit integers (using rand()), and possibly 1 more for
the
N%16 remaining bits (which is masked off the apprograte number of high
bits
to get correct number of bytes), and then append them into a double by
shifting and adding. Then divide by a double version of (2^N - 1) and that
should (for some values of N) produce the closed interval random
floating-point number expressed as a double!

The seed is nor just srand()...

Thanx again...! :)

[==Peteroid==]

"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:uM******** ******@TK2MSFTN GP14.phx.gbl...
Peteroid wrote:
> I know how to use rand() to generate random POSITIVE-INTEGER numbers.
>
> But, I'd like to generate a random DOUBLE number in the range of 0.0
> to 1.0 with resolution of a double (i.e., every possible double value
> in the range could come up with equal probability). I'd also like to
> be able to seed this generator (e.g., via the clock) so that the same
> sequence of random values don't come up every time.


A double in [0.0,1.0) has 2^52 distinct values. To generate such
doubles,
first generate a random integer in [0,2^52) and then divide it by 2^52.
Note that all such integers, including 2^52 can be represented exactly as
doubles.

You might want to look at boost::random (see
http://www.boost.org/libs/random/index.html for details) for
pseudo-random
generators that are good enough to generate 2^52 numbers without cycles.
You need to use a long period generator such as a Mersenne twister to

really
get 2^52 values out of it. Your generator will have to generate 64 bit
values of course.

-cd


Nov 17 '05 #5
Thanks for the link Tom!

As it turns out (and unlike what I first asked), I don't actaully need for
every double floating-point number to be generated randomly that exists in
[0.0,1.0]. It turns out I'm able to determine the 'resolution' of the
generator that is good enough for my application for what it happens to be
doing at the moment. By 'resolution' I mean the number of possible
equally-spaced floating-point random values I need to generate in the
[0.0,1.0] range (e.g., resolution 3 would generate only these values: 0.0,
..5, 1.0).

So I created a class with a 'base' (= resolution-1) and let rand() generate
a random number from 0 to base, and then produce this number divided by
base to get it into the [0.0,1.0] range. This is sufficient for my
application.

I realized, based on the responses here, that any random number generator on
a digital computer will never return all possibly real numbers, so I had to
deal with a 'resolution' no matter what. The method I've created deals with
such any (not too big) resolution 'perfectly', and so it works!

Thanks!

[==Peteroid==]

"TOM" <no****@noprovi der.nodomain> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
You may want to read the section on random number generators in
"Numerical Recipes in C". The text is free and on-line in PDF format by
permission of the publisher, at:

http://www.nr.com/

It's very difficult to build a good random number generator. The text
illustrates the problems and some ways around it, but is focused
on float rather than double.

-- Tom

"Peteroid" <pe************ @msn.com> wrote in message
news:u2******** ******@TK2MSFTN GP11.phx.gbl...
Thanks, Carl!

You gave me a better way than I was going. I'll create a class that allows me to generate a floating-point number in the range of [0.0.,1.0] (yes,
closed on both sides) of arbitray bit-count resolution up to a limit (51, or
possibly even 63, sounds good, based on your response).

Say the bit-count is N. Generate an N-bit random number by generating N/16 number of random 16-bit integers (using rand()), and possibly 1 more for
the
N%16 remaining bits (which is masked off the apprograte number of high
bits
to get correct number of bytes), and then append them into a double by
shifting and adding. Then divide by a double version of (2^N - 1) and that should (for some values of N) produce the closed interval random
floating-point number expressed as a double!

The seed is nor just srand()...

Thanx again...! :)

[==Peteroid==]

"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam> wrote in message news:uM******** ******@TK2MSFTN GP14.phx.gbl...
Peteroid wrote:
> I know how to use rand() to generate random POSITIVE-INTEGER numbers.
>
> But, I'd like to generate a random DOUBLE number in the range of 0.0
> to 1.0 with resolution of a double (i.e., every possible double value
> in the range could come up with equal probability). I'd also like to
> be able to seed this generator (e.g., via the clock) so that the same
> sequence of random values don't come up every time.

A double in [0.0,1.0) has 2^52 distinct values. To generate such
doubles,
first generate a random integer in [0,2^52) and then divide it by 2^52.
Note that all such integers, including 2^52 can be represented exactly as doubles.

You might want to look at boost::random (see
http://www.boost.org/libs/random/index.html for details) for
pseudo-random
generators that are good enough to generate 2^52 numbers without cycles. You need to use a long period generator such as a Mersenne twister to

really
get 2^52 values out of it. Your generator will have to generate 64 bit
values of course.

-cd



Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3701
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a specified range), random strings (specifying allowed characters and a distribution of lengths), or choose randomly between several generators (for better control of the distribution of values). Is there any library for this sort of thing in Python?...
10
2909
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I have is that the first loop I get a sequence of random numbers untuil I get a match, BUT then on the following loops I get the SAME random(?) sequence. I am using rand(). I do not want to get too fancy with the random number generator, but is there...
104
5172
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a better way than to fill an array of range 0... RAND_MAX with pre-computed primes and using the output of rand() to index into it to extract a random prime.
12
5228
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's RAND(). any ideas? I suppose I could use the current rancom number as the seed for the next random number. but would that really work?
13
3196
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister, with it's massive period of 2^19937-1. Will you only ever have access to a tiny portion of this ring of numbers, if you only use a 32-bit seed? Will this set of sequences be confined to the beginning of the period, ie, your sequence will have to...
7
2208
by: thisismyidentity | last post by:
Hi all, I am trying to predict the behaviour of floating point load and store operations on integer locations. I ve written a small piece of code having some inline assembly code which I am describing here. ======================================================== #include<stdio.h> #include<stdlib.h>
3
4685
by: Daniel | last post by:
Hey guys Using Random(), how random is it, is it possible to be predicted? I have a requirement for a very good random number generator. I was doing something such as: Random randomSeed = new Random((int)_seedTimer.ElapsedTicks); _random = new Random(randomSeed.Next(0,99999999)); return random.Next(min, max);
21
13513
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
11
3022
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays of values. I get a random index value for each array so I can pull the data from them.
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.