GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Feb-2007, 12:25
corruptjedi corruptjedi is offline
New Member
 
Join Date: Feb 2007
Posts: 1
corruptjedi is on a distinguished road

tricky random gen problem


I have to create a function that will take in an upper and lower bound and then generate a random number. The problem is the bounds are doubles and one of the bounds I have to test this with is lb=2.3 and ub=2.35. I'm stuck and I don't know where to go from here.
  #2  
Old 06-Feb-2007, 12:41
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: tricky random gen problem


Quote:
Originally Posted by corruptjedi
I have to create a function that will take in an upper and lower bound and then generate a random number. The problem is the bounds are doubles and one of the bounds I have to test this with is lb=2.3 and ub=2.35. I'm stuck and I don't know where to go from here.

The standard library function rand() returns an integer number greater than or equal to zero and less than or equal to RAND_MAX, where RAND_MAX has an implementation-dependent integer value, and is defined in <stdlib.h>.

To generate a double number that is greater than or equal to 0.0 and less than or equal to 1.0:

CPP / C++ / C Code:
#include <stdlib.h>
.
.
.
    double x;
.
.
.
    x = (double)rand() / ((double)RAND_MAX + 1.0);
.
.
.

Now if you want a double in some range other than [0, 1], apply scaling and translation to the expression for x.

For example, to get a double number in the range [0, 2], then you can obtain x as above then
CPP / C++ / C Code:
double y;
.
.
.
   y = x * 2.0;
.
.
.

To get a double number in the range [1, 3], then

CPP / C++ / C Code:
double z;
.
.
.
    z = 1.0 + x * 2.0;
.
.
.

Of course the call to rand() and conversion to your range can be done all in one statement, but I like to keep them separate just to make it easier for me to observe correctness.


Regards,

Dave

Footnote: This is valid C code and is not illegal in C++, but for C++, you really should #include <cstdlib> instead of <stdlib.h>. Use of C-style casts in this case is not dangerous, but C++ programmers often recommend using new-style casting throughout your code (static_cast<double>() ). (The important thing is that you must use some kind of cast(s) to force floating point arithmetic for every sub-expression.) The way that I have written the expression for x is will always work.
  #3  
Old 06-Feb-2007, 14:37
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: tricky random gen problem


Quote:
Originally Posted by davekw7x
To generate a double number that is greater than or equal to 0.0 and less than or equal to 1.0:

CPP / C++ / C Code:

    x = (double)rand() / ((double)RAND_MAX + 1.0);


Correction:

The above expression results in values of the double variable x that are greater than or equal to 0.0 and less than 1.0

If you want a double value that is less than or equal to 1.0, then you can use
CPP / C++ / C Code:
    x = (double)rand() / (double)RAND_MAX;

Note that for this expression one of the (double) casts can be omitted (and the other operand will be promoted to a double automatically), but I usually just leave them both as shown.

In general if lower is the lower bound and upper is the upper bound, then to get values that are greater than or equal to the lower bound and less than or equal to the upper bound:

CPP / C++ / C Code:
double x;
double lower;
double upper;
.
.
.
        x = lower + (upper - lower) * (double)rand() / ((double)RAND_MAX);
.
.

Regards,

Dave
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Random number generator problem... icondor C Programming Language 2 12-Oct-2006 08:28
random access file read problem wmmccoy0910 C Programming Language 13 19-Aug-2006 03:02
random number generation issues Jonnyz007 C++ Forum 8 27-Oct-2005 20:13
Need Help with my Cards Program (C++) krisopotamus C++ Forum 2 06-Oct-2005 17:48
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 08:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:35.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.