tags:

views:

115

answers:

2

How do I generate random chars and integers within a method so that the method can be called in main() and so that the method generates random chars and integers together. I do not want a method that genrates chars and another methods that generates integers.

+4  A: 

You can write a method like (assuming you want only lower case English characters, you can extend it):

void generate(char& ranChar, int& ranNmber)
{
  //Generate a random number in the range 0-25 and add the ascii value 'a'
  ranChar = rand() % 26 + 'a';
  ranNumber = rand();
}

int main()
{
   //Seed the random number generator with the current time
   srand(time(NULL));
   char ch;
   int n= 0;
   generate(ch,n);
   return 0;
}
Naveen
What is the 27th letter of the English alphabet? :D
Jonathan Leffler
I'm getting the error, 'time': identifier not found
Mohit Deshpande
#include <ctime>
Naveen
A: 

You could use boost::tuple, like this:

boost::tuple<int, char> gen () {
// srand() etc
return make_tuple(rand(), (rand() % ('z' - 'a' + 1)) + 'a');
}
rmn