0 Replies - 2512 Views - Last Post: 24 July 2011 - 06:42 PM Rate Topic: -----

#1 stackoverflow   User is offline

  • D.I.C Addict
  • member icon

Reputation: 166
  • View blog
  • 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   User is offline

  • D.I.C Addict
  • member icon

Reputation: 166
  • View blog
  • 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;
}

Was This Post Helpful? 0
  • +
  • -

#3 stackoverflow   User is offline

  • D.I.C Addict
  • member icon

Reputation: 166
  • View blog
  • 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;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1