Question:
How can you generate a random number in c++ from a list of selected numbers?
Geetika S
2010-08-28 03:54:48 UTC
I am making a game in which I want to generate a random vowel from the five vowels. How will I be able to do that in C++?
Four answers:
Cubbi
2010-08-28 08:19:55 UTC
In modern C++, you can use uniform int distribution:

(this will work in gcc 4.4 and MSVC 2010 as-is, but 2005-2010 compilers may need )



#include

#include

#include

int main()

{

     const std::string vowels = "aeiou";

     std::random_device rd;

     std::mt19937 e(rd());

     std::uniform_int_distribution<> u(0, vowels.size()-1);

     std::cout << "random vowel: " << vowels[u(e)] << '\n';

}



in the 1998-compatible C++, random numbers are only available through the C library, as already mentioned:



#include

#include

#include

#include

int main()

{

     std::srand(std::time(NULL));

     const std::string vowels = "aeiou";

     std::cout << "random vowel: " << vowels[std::rand()%vowels.size()] << '\n';

}
Ciaron
2010-08-28 13:20:40 UTC
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<<"First vowel: "<< vowels[rand() % 4]<
srand ( time(NULL));

cout<<"Random vowel:"<< vowels[rand() % 4]<
srand ( 1 );

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:



#include

#include

#include

int main(void)

{

const char vowels[5]={'a','e','i','o','u'};

srand(time(NULL));

for(int i=0; i<4; i++) printf("%c", vowels[rand()%5]);

printf("\n");

}
ankit
2010-08-28 11:02:41 UTC
look into this



http://www.cplusplus.com/forum/general/14192/


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...