Can someone help me fix this code?

I'm supposed to write a program that tests the “randomness” of the C++ random number generator.
Your program should generate 10,000 random numbers between 0 and 99. It would keep track of how many times each number was generated.
Have the program report the following information:
 Median number of times each number was generated
 Maximum times a number was generated (and which numbers they were)
 Minimum times a number was generated (and which numbers they were)
I don't understand what I did below. I tried to generate random numbers, put them into an array and I made a function to find the median. However I have no clue how to figure out max/min times a number was generated and which they were and also I don't know how to fix the code I have so far. Thanks in advance!

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

using namespace std;
int numbers[100]; //to store numbers from 0 - 99

int main()
{
    
    for(int i =0; i<10000; i++)
    {
       int number = rand() % 10000 + 1;
        numbers[number]=0; //Fill with zeros
    }
    
    srand(time(NULL));
    
    for(int i =0; i<10000; i++)
    {
        int number = rand() % 10000 + 1;
        
        numbers[number]++; //To count each time a number appears
        
        int temp=0;
        for(int i =0; i<100; i++)
        {
            temp+= numbers[number]; //count numbers
            if(temp>=5000)
            {//you found the mid value on this i index
                cout << "The median is " << i << endl;
                
                i = 100; //to break the cycle, you found the median number
                int countNumbers[100][10]; // 100 to store number from 0-99 and the 10 to store the value of each generation cycle.
                

            }
    }
    }
    return 0;
}
Last edited on
line 6, int numbers[100]; //to store numbers from 0 - 99 here you need array size of 10,000


line13, int number = rand() % 10000 + 1;
here you need 0 to 99, but you did 1 to 10,000


line14, numbers[number]=0; //Fill with zeros
what is this ¿ ¡ ? ¿

line17, srand(time(NULL));
you used srand after rand ?

ok, enough for me...
anup: thank you for helping me with some of that. I'm really confused so i appreciate any help i can get
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <ctime>
#include <cstdlib>

int main(){	

	//freopen("out.txt","w",stdout); // to write in file
	srand(time(NULL));
	int arr[100];
	
	for(auto &x:arr){
		x=0;
	}
	
	for(int i=0; i<10000; i++){
		arr[rand()%100]++;
	}
	
	for(int i=0; i<100; i++){
		std::cout << "Number " << i << ",  Occurences = " << arr[i] << " times.\n";
	}

return 0;	
}


Number 0,  Occurences = 105 times.
Number 1,  Occurences = 93 times.
Number 2,  Occurences = 100 times.
Number 3,  Occurences = 88 times.
Number 4,  Occurences = 109 times.
Number 5,  Occurences = 93 times.
Number 6,  Occurences = 98 times.
Number 7,  Occurences = 103 times.
Number 8,  Occurences = 105 times.
Number 9,  Occurences = 79 times.
Number 10,  Occurences = 95 times.
Number 11,  Occurences = 105 times.
Number 12,  Occurences = 107 times.
Number 13,  Occurences = 102 times.
Number 14,  Occurences = 114 times.
Number 15,  Occurences = 97 times.
Number 16,  Occurences = 113 times.
Number 17,  Occurences = 90 times.
Number 18,  Occurences = 79 times.
Number 19,  Occurences = 100 times.
Number 20,  Occurences = 97 times.
Number 21,  Occurences = 93 times.
Number 22,  Occurences = 99 times.
Number 23,  Occurences = 96 times.
Number 24,  Occurences = 89 times.
Number 25,  Occurences = 111 times.
Number 26,  Occurences = 85 times.
Number 27,  Occurences = 97 times.
Number 28,  Occurences = 121 times.
Number 29,  Occurences = 95 times.
Number 30,  Occurences = 98 times.
Number 31,  Occurences = 95 times.
Number 32,  Occurences = 100 times.
Number 33,  Occurences = 90 times.
Number 34,  Occurences = 107 times.
Number 35,  Occurences = 105 times.
Number 36,  Occurences = 93 times.
Number 37,  Occurences = 85 times.
Number 38,  Occurences = 95 times.
Number 39,  Occurences = 103 times.
Number 40,  Occurences = 108 times.
Number 41,  Occurences = 100 times.
Number 42,  Occurences = 86 times.
Number 43,  Occurences = 102 times.
Number 44,  Occurences = 115 times.
Number 45,  Occurences = 98 times.
Number 46,  Occurences = 115 times.
Number 47,  Occurences = 104 times.
Number 48,  Occurences = 92 times.
Number 49,  Occurences = 121 times.
Number 50,  Occurences = 111 times.
Number 51,  Occurences = 112 times.
Number 52,  Occurences = 91 times.
Number 53,  Occurences = 103 times.
Number 54,  Occurences = 99 times.
Number 55,  Occurences = 101 times.
Number 56,  Occurences = 110 times.
Number 57,  Occurences = 107 times.
Number 58,  Occurences = 97 times.
Number 59,  Occurences = 112 times.
Number 60,  Occurences = 96 times.
Number 61,  Occurences = 106 times.
Number 62,  Occurences = 95 times.
Number 63,  Occurences = 113 times.
Number 64,  Occurences = 93 times.
Number 65,  Occurences = 111 times.
Number 66,  Occurences = 98 times.
Number 67,  Occurences = 86 times.
Number 68,  Occurences = 110 times.
Number 69,  Occurences = 109 times.
Number 70,  Occurences = 100 times.
Number 71,  Occurences = 114 times.
Number 72,  Occurences = 109 times.
Number 73,  Occurences = 112 times.
Number 74,  Occurences = 94 times.
Number 75,  Occurences = 89 times.
Number 76,  Occurences = 94 times.
Number 77,  Occurences = 96 times.
Number 78,  Occurences = 81 times.
Number 79,  Occurences = 100 times.
Number 80,  Occurences = 85 times.
Number 81,  Occurences = 89 times.
Number 82,  Occurences = 95 times.
Number 83,  Occurences = 95 times.
Number 84,  Occurences = 116 times.
Number 85,  Occurences = 100 times.
Number 86,  Occurences = 101 times.
Number 87,  Occurences = 83 times.
Number 88,  Occurences = 92 times.
Number 89,  Occurences = 103 times.
Number 90,  Occurences = 98 times.
Number 91,  Occurences = 114 times.
Number 92,  Occurences = 119 times.
Number 93,  Occurences = 96 times.
Number 94,  Occurences = 106 times.
Number 95,  Occurences = 115 times.
Number 96,  Occurences = 77 times.
Number 97,  Occurences = 94 times.
Number 98,  Occurences = 108 times.
Number 99,  Occurences = 95 times.
so what you're doing is reading in a file? is there a way to do it without reading in a file?
no, i wrote to file to show the output.
when line7 is deactivated it will show result in your console screen.
Last edited on
thank you so much! i am so thankful to you. does the median formula i had work in this?
what is your median formula?
1
2
3
4
5
6
7
8
for(int i =0; i<100; i++)
        {
            temp+= numbers[number]; //count numbers
            if(temp>=5000)
            {//you found the mid value on this i index
                cout << "The median is " << i << endl;
                
                i = 100; //to break the cycle, you found the median number 
Topic archived. No new replies allowed.