# Random even or odd number generators

Page 1 of 1

## 0 Replies - 2969 Views - Last Post: 24 July 2011 - 06:42 PMRate Topic:     //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=364360&amp;s=a00f2bd425ed5c93884b372d4507766b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 stackoverflow Reputation: 166
• Posts: 545
• Joined: 06-July 11

# Random even or odd number generators

Posted 24 July 2011 - 06:42 PM

Description: The functions assume the range is correct. A sample usage demonstrates how one may generate a collection of random even or odd numbers. These are two functions you can use to generate random even or odd numbers within a range.
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*
*  randomEven - returns an even number within a specified range
*  @base: the lowest possible value
*  @limit: the upper possible value
*  @return: returns an even integer within the range
*/
int randomEven(int base, int limit)
{
int even = 0;
while ((((even = rand() % limit - base + 1) + base) % 2))
;
return even;
}

/*
*  randomOdd - returns an odd number within a specified range
*  @base: the lowest possible value
*  @limit: the upper possible value
*  @return: returns an odd integer within the range
*/
int randomOdd(int base, int limit)
{
int odd = 0;
while (!(((odd = rand() % limit - base + 1) + base) % 2))
;
return odd;
}

/* sample usage below */

int main(void)
{
srand(time(NULL));

printf("Random numbers between 1 and 10nn");
int index;
for(index = 0; index < 20; index++){
printf("An even is %dn", randomEven(0, 10));
printf("An odd is  %dn", randomOdd(0, 10));
}

return 0;
}
```

Is This A Good Question/Topic? 0

## Replies To: Random even or odd number generators

### #2 stackoverflow Reputation: 166
• Posts: 545
• Joined: 06-July 11

## Re: Random even or odd number generators

Posted 24 July 2011 - 06:42 PM

Description: The functions assume the range is correct. A sample usage demonstrates how one may generate a collection of random even or odd numbers. These are two functions you can use to generate random even or odd numbers within a range.
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*
*  randomEven - returns an even number within a specified range
*  @base: the lowest possible value
*  @limit: the upper possible value
*  @return: returns an even integer within the range
*/
int randomEven(int base, int limit)
{
int even = 0;
while ((((even = rand() % (limit - base + 1) + base)) % 2))
;
return even;
}

/*
*  randomOdd - returns an odd number within a specified range
*  @base: the lowest possible value
*  @limit: the upper possible value
*  @return: returns an odd integer within the range
*/
int randomOdd(int base, int limit)
{
int odd = 0;
while (!(((odd = rand() % (limit - base + 1) + base)) % 2))
;
return odd;
}

/* sample usage below */

int main(void)
{
srand(time(NULL));

printf("Random numbers between 1 and 10nn");
int index;
for(index = 0; index < 20; index++){
printf("An even is %dn", randomEven(0, 10));
printf("An odd is  %dn", randomOdd(0, 10));
}

return 0;
}
```

### #3 stackoverflow Reputation: 166
• Posts: 545
• Joined: 06-July 11

## Re: Random even or odd number generators

Posted 24 July 2011 - 06:42 PM

Description: The functions assume the range is correct. A sample usage demonstrates how one may generate a collection of random even or odd numbers. These are two functions you can use to generate random even or odd numbers within a range.
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*
*  randomEven - returns an even number within a specified range
*  @base: the lowest possible value
*  @limit: the upper possible value
*  @return: returns an even integer within the range
*/
int randomEven(int base, int limit)
{
int even = 0;
while ((((even = rand() % (limit - base + 1) + base)) % 2))
; /* don't remove */
return even;
}

/*
*  randomOdd - returns an odd number within a specified range
*  @base: the lowest possible value
*  @limit: the upper possible value
*  @return: returns an odd integer within the range
*/
int randomOdd(int base, int limit)
{
int odd = 0;
while (!(((odd = rand() % (limit - base + 1) + base)) % 2))
; /* don't remove */
return odd;
}

/* sample usage below */

int main(void)
{
srand(time(NULL));

printf("Random numbers between 1 and 10nn");
int index;
for(index = 0; index < 20; index++){
printf("An even is %dn", randomEven(0, 10));
printf("An odd is  %dn", randomOdd(0, 10));
}

return 0;
}
```

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }