Random Number, Odd, Even

Hello, I am working on a random number generator which then tells you whether the number generated is even or odd. This issue I am having is with the even or odd portion. I cannot figure out why it is skipping over my first if. Thank you.


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 <time.h>
#include <stdlib.h>
using namespace std;

int main() {

    int iseed = time(NULL);
    srand (iseed);
    int min = 10;
    int max = 25;
    int randomNum = min + rand()%16;
    cout << "Random number between 10 and 25 = " << randomNum << "\n";

    if ( min + rand()%16 == 0 )
    {
        cout << "The random number " << randomNum << "even.\n";
    }
    else if ( min + rand()%16 != 0 )
    {
        cout << "The random number is " << randomNum << ": odd.\n";
    }
    return 0;
}



This is what its outputting:

1
2
Random number between 10 and 25 = 14
The random number is 14: odd.

Last edited on
Probably because you're calling rand() three times instead of just once, so you really don't know what random number the if() statement is using. Also because you're calling rand() in the else if() you're lucky that even printed that it the number was odd. You should be using randomNum, in the if statement, not calling rand() and you really should only be using an else statement not if else().


rand() % 16 will produce a number in the range 0 to 15 inclusive. You are adding 10 to this number. So the range is 10 to 25. You are then testing for equality to 0!!!! min + rand()%16 will never, ever be 0 - hence the first if is always skipped.

To test if an integer number num is even then use:

 
num % 2 == 0


To test if a number is odd use:

 
num % 2 != 0


Note that an integer number is either odd or even and cannot be both! So only 1 test is needed.
Okay I modified it and tested it many times and it appears to be functioning correctly. Seeing any errors in this? For some reason if I use just and else instead of an else if it breaks the program.

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 <time.h>
#include <stdlib.h>
using namespace std;

int main() {

    int iseed = time(NULL);
    srand (iseed);
    int min = 10;
    int max = 25;
    int randomNum = min + rand()%16;
    cout << "Random number between 10 and 25 = " << randomNum << "\n";

    if ( randomNum %2 == 0 )
    {
        cout << "The random number " << randomNum << " even.\n";
    }
    else if ( randomNum %2 != 0 )
    {
        cout << "The random number is " << randomNum << ": odd.\n";
    }
    return 0;
}
if and else go together. you can't say else / else in c++, if that is what you said you tried.

its fine but a little redundant and the english is inconsistent.
both should give the same output except the final word, right? They do not.
also, its "the random number " << number << " is " << ... your line 21 would print "the random number is 5 odd" instead of number 5 is odd

else is implied here, so you can also debloat.

1
2
3
4
5
6
7
8
9
 if ( randomNum %2 == 0 )
    {
        cout << "The random number " << randomNum << " even.\n";
    }
    else if ( randomNum %2 != 0 ) //redundant
    {
        cout << "The random number is " << randomNum << ": odd.\n";
    }


or the whole thing can be done with:
1
2
3
string evenodd[] = {" is even\n"," is odd\n"); //note the spaces and end of lines padded in here.
   cout << "The random number " << randomNum <<  evenodd[randomNum %2];
Last edited on
I tried to do

1
2
3
4
5
6
7
8
if ( randomNum %2 == 0 )
    {
        cout << "The random number " << randomNum << " even.\n";
    }
    else ( randomNum %2 != 0 ) /redundant
    {
        cout << "The random number is " << randomNum << ": odd.\n";
    }


This didnt work, but I see now I should have gotten rid of the ( randomNum %2 != 0 ) /redundant and it would have worked.

Also will fix the English errors. Thank you very much.
I would also recommend you use your variables instead of number literals as much as possible. I would change lines 12 and 13:
12
13
    int randomNum = min + rand()% (max - min + 1);
    cout << "Random number between " << min << " and " << max << " = " << randomNum << "\n";

This makes changing the program to cover a different range of numbers easier.
Thats a great idea, thank you.
second one I showed above is a little advanced. making less code is not always making better code, but as you saw, repeating the same text with slight variations has its own tendency to produce minor variation 'bugs' so the idea is to type the string one time (there are a couple of ways to do that).
Topic archived. No new replies allowed.