Rounding a "double" number without using "if" statements
double num;
int num_rounded = static_cast<int>(floor(num+0.5));
Showing posts with label Algorithms. Show all posts
Showing posts with label Algorithms. Show all posts
|
Thursday, January 21, 2010
|
| Back to Top |
Uniform (Pseudo) Random [0,1] Probability distribution in C++
Generating Uniform (Pseudo) Random Probability distribution in the interval [0,1] in C++:
double p = (RAND_MAX - rand()) / static_cast<double>(RAND_MAX); --or-- double p = rand() / static_cast<double>(RAND_MAX); Of course, use #include<cstdlib> for using int rand(); function and use void srand(int); to set the seed (if necessary). Debugging tip: --------------- Using a specific seed initially can help with repeating the random number patterns. Eg: srand(5); can be used in the program for debugging purposes and then removed later on!
Labels:
Algorithms
|
| Back to Top |