Random numbers!

Started by
9 comments, last by Galo 20 years, 3 months ago
I use a random header file that I just call when I need random numbers. I put it together using Rob McGregor's book Practical C++.

#ifndef __random_h__#define __random_h__#include <cstdlib>#include <ctime>class CRand{public:	CRand() { srand((unsigned)time(0));}	CRand(unsigned seed) { srand(seed); }	int Generate(int min, int max)	{		if (max < 0)max = 0;		if ((min == max) && (min == 0))			return 0;		return rand() % (max + 1 - min) + min;	}};#endif


When you need a random number of any kind (don't forget to call the CRand rand; class in main) just call rand.Generate(minimum number, maximum number) and it does it for you.

"Your mother was a hamster and your father smelled of elderberries."

[edited by - Nested Block on January 8, 2004 5:51:03 PM]

[edited by - Nested Block on January 8, 2004 6:00:41 PM]

[edited by - Nested Block on January 8, 2004 6:01:32 PM]
"Your mother was a hamster and your father smelled of elderberries."

This topic is closed to new replies.

Advertisement