VB icon

Custom pseudo-random generator class v1.0

Email
Submitted on: 8/20/2016 9:50:20 AM
By: Ben  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++, UNIX C++
Views: 1010
 
     Here is a class I made that allows you to generate random numbers with out using the build in Rand() function. This is no way as a replacement but just shows how to do your own pseudo-random generator, small example at end of class, that you can put into your main project. Hope you find is usfull.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Custom pseudo-random generator class v1.0
// Description:Here is a class I made that allows you to generate random numbers with out using the build in Rand() function. This is no way as a replacement but just shows how to do your own pseudo-random generator, small example at end of class, that you can put into your main project. Hope you find is usfull.
// By: Ben
//**************************************

class TRandom{
private:
	int m_seed;
	int m_Range;
public:
	void SetSeed(int seed);
	void SetMax(int max);
	int Next();
};
void TRandom::SetSeed(int seed){
	m_seed = seed;
}
void TRandom::SetMax(int max){
	m_Range=max;
}
int TRandom::Next(){
	//pseudo-random generator
	//Changeing the variables below may effect the random generator so be careful.
	int m = 647;
	int c = 1;
	int a = 16807;
	//Generator a random number
	m_seed = (m_seed*a+c) % m;
	//Return
	return m_seed % m_Range;
}
//EXAMPLE USING THE CLASS
/*
int main(int argc, char **anvg, char**envp){
	class TRandom MyRand;
	int i = 0;
	//This will make sure random numbers are different each time.
	MyRand.SetSeed(-time(0));
	//The rang of number you want to generate.
	MyRand.SetMax(45);
	//Example
	while(i < 20){
		//Print random number in range of 0-45
		cout << MyRand.Next() << endl;
		i++;
	}
	//Just to keep the console open.
	system("pause");
	return 1;
}
*/


Other 50 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.