Help!
-
I'm trying to learn how to generate a random number (for a jukebox to play MIDI songs). I bought the C++ standard library reference book and it explains what rand( ) does, but I've not been able to get it to work yet. I don't see any complete examples. I have several books, but they don't talk about rand( ). I also have discovered the random_shuffle function as well, but don't know how to make it work. The biggest problem I've had so far with programming is to implement the code for the various functions. Anyway, I was hoping someone(s) would give me a simple working example of both 1. rand( ) 2. random_shuffle( ) I would appreciate it much, Thanks...Dave :-D
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
-
I'm trying to learn how to generate a random number (for a jukebox to play MIDI songs). I bought the C++ standard library reference book and it explains what rand( ) does, but I've not been able to get it to work yet. I don't see any complete examples. I have several books, but they don't talk about rand( ). I also have discovered the random_shuffle function as well, but don't know how to make it work. The biggest problem I've had so far with programming is to implement the code for the various functions. Anyway, I was hoping someone(s) would give me a simple working example of both 1. rand( ) 2. random_shuffle( ) I would appreciate it much, Thanks...Dave :-D
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
// Seed the random number generator srand( time(0) ); // This is the highest number we want const int nMaxNum = 1234; // Generate it int nNum = rand() % nMaxNum + 1; rand() gives us a number between 0 and RAND_MAX (which if i recall correctly is ~32768 on VC6), so to convert it to the range we want, we divide it by the max number and take the remainder. random_shuffle() randomises the order of components in a random access container such as a vector - you should not be using this function to generate a random number, but instead you could insert the records for all the songs into a vector, and then call random_shuffle on the vector to randomise it. e.g. struct Record { string sName; // Data for the song itself }; vector vSongs; // Add some songs vSongs.push_back( record1 ); vSongs.push_back( record2 ); // etc... // Randomise them random_shuffle( vSongs.begin(), vSongs.end() ); Dave http://www.cloudsofheaven.org
-
// Seed the random number generator srand( time(0) ); // This is the highest number we want const int nMaxNum = 1234; // Generate it int nNum = rand() % nMaxNum + 1; rand() gives us a number between 0 and RAND_MAX (which if i recall correctly is ~32768 on VC6), so to convert it to the range we want, we divide it by the max number and take the remainder. random_shuffle() randomises the order of components in a random access container such as a vector - you should not be using this function to generate a random number, but instead you could insert the records for all the songs into a vector, and then call random_shuffle on the vector to randomise it. e.g. struct Record { string sName; // Data for the song itself }; vector vSongs; // Add some songs vSongs.push_back( record1 ); vSongs.push_back( record2 ); // etc... // Randomise them random_shuffle( vSongs.begin(), vSongs.end() ); Dave http://www.cloudsofheaven.org
Thanks Dave, I'll give it a try. I was trying to use the get_time( ) function, but didn't think of doing it the way you did. Is get_time something entirely different? Thanks much, Dave :-D
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
-
Thanks Dave, I'll give it a try. I was trying to use the get_time( ) function, but didn't think of doing it the way you did. Is get_time something entirely different? Thanks much, Dave :-D
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
By the look of things, get_time() retrieves a time from an input stream - but i have never used it so i'm not sure. time() retrieves the time from the system clock instead. Dave http://www.cloudsofheaven.org
-
By the look of things, get_time() retrieves a time from an input stream - but i have never used it so i'm not sure. time() retrieves the time from the system clock instead. Dave http://www.cloudsofheaven.org