Randomly selecting a number from an array based on probability

Currently using an array with probability in a class, but i'm having troubles making a procedure that would show what number would appear based on what random number was selected. I'm having a hard time explaining so if I didn't do a very good job, let me know.

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
  #include <iostream>
#include <time.h>

using namespace std;

class Cspinner
{

private:
	int result;
	int land;
	int apple;
	int orange;
	int cherry;
	int banana;
	int peach;
	int fruit[5] = {30, 55, 75, 90, 100};
public:
	
	Cspinner()
	{
		fruit[0] = apple; // 30% Probability
		fruit[1] = orange; // 25% Probability
		fruit[2] = cherry; // 20% Probability
		fruit[3] = banana; // 15% Probability
		fruit[4] = peach; // 10% Probability
	}

	Cspinner(int Newapple, int Neworange, int Newcherry, int Newbannan, int Newpeach) // User selected Probability
	{
		
		fruit[0] = Newapple;
		fruit[1] = Neworange;
		fruit[2] = Newcherry;
		fruit[3] = Newbannan;
		fruit[4] = Newpeach;

	}

	void spin() // Procedure to spin fruit on spinner
	{
		int fruit[5];

		result = rand() % 5;

	}

	void show() // Procedure to show fruit on spinner
	{
		if (result <= fruit[0])
		{
			land = apple;
		}

		else if (result < fruit[1])
		{
			land = orange;
		}

		else if (result < fruit[2])
		{
			land = cherry;
		}

		else if (result < fruit[3])
		{
			land = banana;
		}

		else if (result <= fruit[4])
		{
			land = peach;
		}
		cout << land << endl;
		//cout << result << endl;
	}
};

void main()
{
	srand(time(NULL));

	Cspinner w1;
	Cspinner w2;
	Cspinner w3(80, 5, 5, 5, 5);
	for (int x = 0; x <= 9; x++)
	{
		w1.spin();
		w2.spin();
		w3.spin();
		w1.show();
		w2.show();
		w3.show();
		cout << endl;
	}
	system("pause");
}

1
2
3
4
5
6
7
Cspinner()
	{
		fruit[0] = apple; // 30% Probability
		fruit[1] = orange; // 25% Probability
		fruit[2] = cherry; // 20% Probability
		fruit[3] = banana; // 15% Probability
		fruit[4] = peach; // 10% Probability 

Notice that you're assigning fruit to values that were never initialized.
Since fruit was initialized in declaration, there is no need for this default constructor.

But I notice that you've written void main. So in that case initializing fruit at declaration would be incorrect right (I'm not sure)? In that case assign integer values to fruit at the default constructor and correct the declaration.

Seems like you have no use for these variables
        int apple;
	int orange;
	int cherry;
	int banana;
	int peach;


1
2
3
4
5
6
7
void spin() // Procedure to spin fruit on spinner
	{
		int fruit[5];

		result = rand() % 5;

	}

Why are you declaring 'fruit[5]' here?



Your probability logic is very incorrect. You need to find the cumulative probability. That is,
if int fruit[5] = {30, 55, 75, 90, 100};
then
cumulative[5] = {30, 85, 160, 250, 300};


And generate a random number from 0 to cumulative[4] that is 300.

Then you check whether the randomly generated number is the ranges [1, 30] [31, 85] [86, 160] [161, 250] [251, 300] using simple if statements. Corresponding range will give you the fruit that has been selected.

To display the probability of getting that fruit simply divide the range's length by cumulative[4].
closed account (E0p9LyTq)
The <random> library has a handy customizable random number distribution: std::discrete_distribution

https://en.cppreference.com/w/cpp/numeric/random/discrete_distribution
Not sure why
int fruit[5] = {30, 55, 75, 90, 100};
Wouldnt work as it seems to be but as for everything else, it seems to be working.
Last edited on
Initializing at declaration is perfectly legal and should work with compilers above C++11. But it's not allowed with older compilers.

Anyways you should use the default constructor that you made like I said and you probably have done that by now.
Topic archived. No new replies allowed.