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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#define _USE_MATH_DEFINES

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <ctime>

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:
1
2
3
overallReliability = analyticalReliabilityOne;
if (analyticalReliabilityTwo <= overallReliability) overallReliability = analyticalReliabilityTwo;
if (analyticalReliabilityThree <= overallReliability) overallReliability = analyticalReliabilityThree;

Topic archived. No new replies allowed.