![]() |
|
#1
|
|||
|
|||
tricky random gen problemI 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
|
|||
|
|||
Re: tricky random gen problemQuote:
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:
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:
To get a double number in the range [1, 3], then CPP / C++ / C Code:
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
|
|||
|
|||
Re: tricky random gen problemQuote:
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:
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:
Regards, Dave |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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