### Random Number Generation for Boolean Variable

Hello. It's me again. I'm experimenting with random generators. I would like to generate a random number between 0 and 1. This will be multiplied with reliabilityOne to output a boolean value of either true or false. However, I cannot get the function to work properly. Any help would be greatly appreciated. The program keeps outputting a value of 1.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124`` ``````#define _USE_MATH_DEFINES #include #include #include #include using namespace std; bool isComponentWorking (double reliabilityOne); int main(){ srand (time(0)); double reliabilityOne; double reliabilityTwo; double reliabilityThree; double numberOfSimulations; double overallReliability; double analyticalReliabilityOne; double analyticalReliabilityTwo; double analyticalReliabilityThree; bool isPlworking; //Request for user input values cout << "Please input a value for the reliability of component 1 (Ex Input :0.80 )\n"; cin >> reliabilityOne; cout << "Please input a value for the reliability of component 2 (Ex Input :0.85 )\n"; cin >> reliabilityTwo; cout << "Please input a value for the reliability of component 3 (Ex Input :0.95 )\n"; cin >> reliabilityThree; cout << "Please input a value for the number of simulations to preform (Ex Input :3 )\n"; cin >> numberOfSimulations; //Analytical Reliability of Components Calculations analyticalReliabilityOne = reliabilityOne * (1 - ((1 - reliabilityTwo) * (1 - reliabilityThree))); analyticalReliabilityTwo = reliabilityTwo * (1 - ((1 - reliabilityOne) * (1 - reliabilityThree))); analyticalReliabilityThree = reliabilityThree * (1 - ((1 - reliabilityTwo) * (1 - reliabilityOne))); //Output of Analytical Reliability and Overall System Reliability cout << "Analytical Reliability of Components\n"; cout << "------------------------------------\n"; cout << analyticalReliabilityOne << endl; cout << analyticalReliabilityTwo << endl; cout << analyticalReliabilityThree << endl << endl; if (analyticalReliabilityOne <= analyticalReliabilityTwo && analyticalReliabilityOne <= analyticalReliabilityThree) { overallReliability = analyticalReliabilityOne; } else if (analyticalReliabilityTwo <= analyticalReliabilityOne && analyticalReliabilityTwo <= analyticalReliabilityThree) { overallReliability = analyticalReliabilityTwo; } else if (analyticalReliabilityThree <= analyticalReliabilityOne && analyticalReliabilityThree <= analyticalReliabilityTwo) { overallReliability = analyticalReliabilityThree; } cout << "Overall Analytical Reliability of System\n"; cout << "----------------------------------------\n"; cout << overallReliability << endl; //isComponentWorking function to test for failure/sucess of a component isPlworking = isComponentWorking(reliabilityOne); isComponentWorking(reliabilityOne); cout << isPlworking; //int i; //for (i > 0; i < numberOfSimulations; i++) //{ //bool isPlworking = isComponentWorking(reliabilityOne); // isComponentWorking(reliabilityOne); // cout << isPlworking << endl; // if (isPlworking == 1) // { // int count; // count = count + 1; // } } bool isComponentWorking (double reliabilityOne) { bool isPlworking; double randomNumber; if (rand() > 0) { randomNumber = 1; } if (rand() <= 0) { randomNumber = 0; } if ((rand() * reliabilityOne) == 0) { isPlworking = false; } else if ((rand() * reliabilityOne) == 1) { isPlworking = true; } return isPlworking; } ``````
Your problem seems to be in isComponentWorking
1. rand() is always greater than 0, so randomNumber is always 1.
2. randomNumber is never used
3. http://www.cplusplus.com/reference/cstdlib/rand/ specifies that rand returns an integer. When multiplied with a double it is very rarely exactly 0 or exactly 1, so most of the time isPlworking is just what your default compiler puts out

Two other small observations:
1. instead of <stdlib.h> use <cstdlib>
2. the code between lines 57 and 69 can be replaced by something simpler:
 ``123`` ``````overallReliability = analyticalReliabilityOne; if (analyticalReliabilityTwo <= overallReliability) overallReliability = analyticalReliabilityTwo; if (analyticalReliabilityThree <= overallReliability) overallReliability = analyticalReliabilityThree;``````

Topic archived. No new replies allowed.