Count even and odd numbers in a random number generator

So I need a program that will say how many even, odd, and "0" numbers there are from a random number generator that generates a set of numbers. I have the generator working fine and i have done programs where a user inputs a set of numbers and it displays the odd and even numbers, but i can not figure out how to make one that actually counts the even and odd numbers and "0's" in a set of numbers from a random number generator. Here is my code for the generator. I feel like maybe there is something simple im missing or i should have done something different with my generator code to make this easier to do. Any help would be great. Thank you.

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

using namespace std;

int main()

{ printf ("The program will generate 5 numbers using a for loop");
    
    srand((unsigned)time(0));
    int random_integer;
    for(int index=0; index<5; index++){
        random_integer = (rand()%20)+2;
        cout << random_integer << endl;
        
    }
}
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
#include <iostream>
#include <cstdlib>
#include <ctime>

int main() {

    const int N = 16'000'000 ;
    std::cout << "The program will generate " << N << " random numbers using a for loop\n\n" ;

    std::srand( (int)std::time(nullptr) ) ;

    int n_even = 0 ;
    int n_odd = 0 ;
    int n_zero = 0 ;

    const int ubound = 20 ;
    for( int i=0; i<N; ++i ) {

        const int random_integer = std::rand() % ubound ; // + 2 ;
        // remove the +2 or we would never get any zeroes

        if( random_integer == 0 ) ++n_zero ;

        if( random_integer%2 == 0 ) ++n_even ; // note: zero is an even number
        else ++n_odd ; // if it is not even, it must be odd
    }

    std::cout << "out of the " << N << " random integers, there were\n\t"
              << n_zero << " zeroes  (mathematical expectation: " << N/ubound << ")\n\t"
              << n_even << " even numbers  (mathematical expectation: " << N/2 << ")\n\t"
              << n_odd << " odd numbers  (mathematical expectation: " << N/2 << ")\n" ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
   const int N = 100;
   const int b = 20;
   int counts[2] = { 0 };
   srand( time( 0 ) );

   for ( int i = 0; i < N; i++ ) 
   {
      int r = rand() % b;
      if ( r ) counts[r%2]++;
   }
   cout << "\nNumber:        " << N
        << "\nEven positive: " << counts[0]
        << "\nOdd positive:  " << counts[1]
        << "\nZero:          " << N - counts[0] - counts[1];
}

Number:        100
Even positive: 47
Odd positive:  49
Zero:          4
Topic archived. No new replies allowed.