What you need to know is how to create random numbers - to do this is to use the rand & the srand function, the short coming is that this only creates pseudo random numbers which initialized using the argument passed as seed.
For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand.
Two different initializations with the same seed, instructs the pseudo-random generator to generate the same succession of results for the subsequent calls to rand in both cases.
If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.
In order to generate random-like numbers, srand is usually initialized to some distinctive value, like those related with the execution time. For example, the value returned by the function time (declared in header ) is different each second, which is distinctive enough for most randoming needs. This short program should give you an idea on how to approach the problem.
// srand example
#include
#include
#include
int main ()
{char vowels[5] ={'a','e','i','o','u'}; // the standard vowels are stored as an array
cout<<"Again the first vowel: "< system ("pause");
return 0;
}
Just for the record it has been run and is for theory.
JoelKatz
2010-08-28 11:01:50 UTC
Read the documentation for the 'random' and 'srandom' functions.
To convert a random integer to one between 0 and 4 inclusive, use '%5'. To convert from a number from 0 to 4 to a vowel, just use the number to select a vowel from an array. Here's an example: