Zufallszahlen erzeugen

  • TRandom.h
#pragma once

class TRandom
{
public:
	TRandom(void);
	~TRandom(void);

	// reset random seed
	void Reset();
	// random number between 0..1
	double GetRandom();
	// dMin >= random number <= dMax
	double GetRandomDouble(double dMin, double dMax);
	// iMin >= random int number <= iMax
	int GetRandomInt(int iMin, int iMax);
	// random bool, true or false
	bool GetRandomBool();
};
  • TRandom.cpp
#include "TRandom.h"
#include <time.h>
#include <stdlib.h>

TRandom::TRandom(void)
{
	srand((unsigned)time(NULL));
}

TRandom::~TRandom(void)
{
}

// reset random seed
void TRandom::Reset()
{
	srand((unsigned)time(NULL));
}

// random number between 0..1
double TRandom::GetRandom()
{
	return (double)rand()/(double)RAND_MAX;
}

// dMin >= random double number <= dMax
double TRandom::GetRandomDouble(double dMin, double dMax)
{
	return dMin + this->GetRandom() * (dMax - dMin);
}

// iMin >= random int number <= iMax
int TRandom::GetRandomInt(int iMin, int iMax)
{
	return iMin + this->GetRandom() * (iMax - iMin);
}

// random bool, true or false
bool TRandom::GetRandomBool()
{
	return (this->GetRandom() < 0.5);
}