# Finding Even/Odd Using Random Integers

Page 1 of 1

## 4 Replies - 6185 Views - Last Post: 12 October 2011 - 03:05 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=250998&amp;s=5ff1ecfc341b540e2c4fdffa048e5f32&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Anday

Reputation: 1
• Posts: 26
• Joined: 19-May 11

# Finding Even/Odd Using Random Integers

Posted 12 October 2011 - 11:06 AM

Hello world,

I am writing a C++ program outside of my class work that takes a user entered maximum range, uses the value for range and generates that many random integers, then determines whether each integer being generated is even or odd. I want to do this using two functions, so I can understand how to write a multi-function program in C++. One function must determine whether the number is even and then main function which handles everything else.

Here is what I have so far:

```/*
Name: Even/Odd Counter
Date: 28/09/11 13:08
Description: User enters a number range and the
program determines the number of odd and even
numbers randomly generated in that range.
*/

#include <iostream>
#include <time.h>
//#include <ctime>
//#include <cstdlib>

using namespace std;

int det_even(int value) {

bool isEven;

if((value % 2) == 0)
isEven = true;
else
isEven = false;

return isEven;

}

int main() {

int range = 0;

cout << "Enter the max range to consider: ";
cin >> range;
cin.get();

int even = 0, odd = 0;
srand(time(NULL));
int num = rand() % range;

for(int i = 0; i < range; i++) {

if (det_even(num))
even++;
else
odd++;

}

cout << endl << "Number of even integers: " << even;
cout << endl << "Number of odd integers: " << odd;

cin.get();
return 0;

}

```

In the for loop I was just testing to see if that would make sure that each integer in range was being tested. I'm quite unsure if it is necessary - though without even or odd would be 1 - or if I am implementing it correctly. What happens in this program right now is the number entered in range is being printed randomly in the variables even or odd. For example, if the user enters 100 for range, the values for even or odd would be 100. I'm thinking that this has something to do with only one integer being checked in the random integers generated for whether it is even or not. Overall, I'm almost positive that my main issue is how I am handling the random integers. Any direction would be appreciated.

Thanks,
Anday

Is This A Good Question/Topic? 0

## Replies To: Finding Even/Odd Using Random Integers

### #2 Bench

• D.I.C Lover

Reputation: 944
• Posts: 2,464
• Joined: 20-August 07

## Re: Finding Even/Odd Using Random Integers

Posted 12 October 2011 - 11:33 AM

your num doesn't change anywhere in your loop, so all you're doing is repeating exactly the same check for the same number at every iteration

### #3 Anday

Reputation: 1
• Posts: 26
• Joined: 19-May 11

## Re: Finding Even/Odd Using Random Integers

Posted 12 October 2011 - 01:23 PM

Alright, so I defined num and seeded rand() inside my loop instead of outside and now it appears to work fine only when debugging. Otherwise even or odd will be the entire range value. Is the program not considering the det_even function when running, but it is while debugging? Or is there something else going on altogether?

This post has been edited by Anday: 12 October 2011 - 01:25 PM

### #4 CTphpnwb

• D.I.C Lover

Reputation: 3746
• Posts: 13,621
• Joined: 08-August 08

## Re: Finding Even/Odd Using Random Integers

Posted 12 October 2011 - 02:09 PM

Call srand only once, at the beginning.

### #5 Anday

Reputation: 1
• Posts: 26
• Joined: 19-May 11

## Re: Finding Even/Odd Using Random Integers

Posted 12 October 2011 - 03:05 PM

Yes, that was the problem. Thanks for the help!