Random Number vector

I need to know how to take the variable I have for random to check it's value and say how many times each number 0-9 occur within the 500 generated numbers

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
#include<iostream>
#include<vector>
using namespace std;

 vector<int>random()
 {
	vector<int>num(500);//this holds the 500 random numbers
	for (int i=0; 500>i; i++)// generates 500 random numbers and stores them in rawr
	{
		num[i]=(rand()%10);

	}
	return(num);
 void occ(vector<int>&random)//takes in the numbers
 {
	 int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0, j=0;//counters for 0-9
	 vector<int>mult(10); 
	 for (int t = 0; t < mult; t++)//count number of occurences
	 {
	 if (random==0)//adds one to the number of occurences 
		 a++;
	 if (random==1)//adds one to the number of occurences 
		 b++;
	 if (random==2)//adds one to the number of occurences 
		 c++;
	 if (random==3)//adds one to the number of occurences 
		 d++;
	 if (random==4)//adds one to the number of occurences 
		 e++;
	 if (random==5)//adds one to the number of occurences 
		 f++;
	 if(random==6)//adds one to the number of occurences 
		 g++;
	 if (random==7)//adds one to the number of occurences 
		 h++;
	 if (random==8)//adds one to the number of occurences 
		 i++;
	 if (random==9)//adds one to the number of occurences 
		 j++;
	 
 }

 int main()
 {
	 int un;
	 vector<int>random(500);

	 return(0);
 }
Last edited on
This is a straightforward implementation.

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
 
#include <iostream>
#include <cstdlib>
#include <vector>
#include <ctime>


using std::cout;
using std::endl;


int main()
{
    std::vector <int> digits(500);
    std::vector <int> occurances(10);

    srand(time(0));

    for (int i = 0; i < 500; ++i)
        digits[i] = rand() % 10;

    for (int i = 0; i < 500; ++i)
        switch (digits[i])
        {
            case (0):
                occurances[0] += 1;
                break;

            case (1):
                occurances[1] += 1;
                break;

            case (2):
                occurances[2] += 1;
                break;

            case (3):
                occurances[3] += 1;
                break;

            case (4):
                occurances[4] += 1;
                break;

            case (5):
                occurances[5] += 1;
                break;

            case (6):
                occurances[6] += 1;
                break;

            case (7):
                occurances[7] += 1;
                break;

            case (8):
                occurances[8] += 1;
                break;

            case (9):
                occurances[9] += 1;
                break;
        }

    for (int i = 0; i < 10; ++i)
        cout << occurances[i] << endl;

    return (0);
}
Define an array of 10 integers, to use for counting the number of occurrences. Initialise these to zero.

Then use each generated random number, which will be in the range 0-9, as an index to access the appropriate counter, and increment it.

Finally loop through this array of counters and print out the results.

Note, you don't actually need the vector to store your 500 random values, you can simply count them on the fly as they are generated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>

int main()
{
    int counts[10] = { 0 };      // set counts to zero

    for (int i=0; i<500; i++)
    {
        int r = rand() % 10;     // generate random 0-9
        ++counts[r];             // increment count
    }

    for (int i=0; i<10; i++)     // output results
        std::cout << i << " occurred " << counts[i] << " times" << std::endl;

    return 0;
}

Last edited on
So I added an array of 10 integers to the beginning of my occ function how would i call my random numbers to run through my loop?
Topic archived. No new replies allowed.