Okay, I am still new at C++, but I though I'd share this. Maybe it will be useful to others
I wrote 3 functions to generate and return true random numbers. I would like to add that, the numbers you pass to these functions DO NOT require you to add an extra number like you normally would do with random number equations.
Required Includes:
This first function will take in the highest number you want it to allow to generate, then return the random number as an integerCode:#include <iostream> #include <windows.h> #include <vector> #include <boost/lexical_cast.hpp>
This next function will take in an integer and return a string of the random number, to save you the work of converting it.Code:int RandomIntAsInt(int UpperBound) { UpperBound++; srand(GetTickCount()); int random; random = rand()%UpperBound; return random; }
This last function takes in the max you want the random number to get to, and how many numbers you want to generate. It then returns a string with each number spaced apart with 1 space.Code:string RandomIntAsString(int UpperBound) { UpperBound++; srand(GetTickCount()); int random; random = rand()%UpperBound; string total = ""; total = boost::lexical_cast<std::string>(random); return total; }
NOTE: I am still a n00b at C++, so I don't have anything awesome to share. I just thought someone may benefit from thisCode:string RandomNumbersString(int UpperBound, int TotalNumbers) { srand(GetTickCount()); int loop = 0; vector<int> random(TotalNumbers); int N = 0; for(loop = 0; loop < TotalNumbers; loop++){ random[N] = rand()%UpperBound; N++; } string temp = ""; string total = ""; int stringloop; temp = boost::lexical_cast<std::string>(random[0]); total = total + temp; N = 1; --TotalNumbers; for(stringloop = 0; stringloop < TotalNumbers; stringloop++){ temp = boost::lexical_cast<std::string>(random[N]); total = total + " " + temp; N++; } return total;![]()





Reply With Quote