rand.cpp

来自「运用扫描法求设计一个最佳比例的聚光腔」· C++ 代码 · 共 77 行

CPP
77
字号
/* Source File: CRand
   Author: Pieter Claassens (pc@nanoteq.com)
   Date Written: 2002/05/21
*/

#include "stdafx.h"
#include <time.h>
#include "Rand.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


// Default contructor

CRand::CRand(int a, int b) 
{
	SetInterval(a,b);
}

// Assign Low And High Values to class CRand data members. Use Defaults on error.

void CRand::SetInterval(int a, int b) 
{
	if (a > b) 
	{
		AfxMessageBox("Error: HIGH value cannot be less than LOW value!\nUsing defaults: High = 1, Low = 0");
		Low = 0;
		High = 1;
	} 
	else 
	{
		Low = a;
		High = b;
	}
}

// Set the seed to generate pseudo-random numbers. Use the current time & date as seed.

void CRand::SetTimerSeed() 
{
	time_t SeedTime;
	struct tm SeedDate;
	SeedTime = time(0);
	SeedDate = *localtime(&SeedTime);
	int FinalSeed = SeedTime + SeedDate.tm_mday + (SeedDate.tm_mon+1) + (SeedDate.tm_year+1900);
	srand((unsigned int) FinalSeed);
}

// Return the Random number, brief explanation on the method
// Interval : Self explanatory - Get the difference between the High and Low Values 
// RandomOffset = using rand() function to generate random numbers from 0 to Interval-1
// RandomNumber = Add the RandomOffset to the lowest number.

int CRand::DrawRandomNumber() 
{
	int Interval = GetHigh() - GetLow() + 1;
	int RandomOffset = rand() % Interval;
	int RandomNumber = GetLow() + RandomOffset;
	return RandomNumber;
}

// Standard Inpectors - Return High & Low values

int CRand::GetHigh()
{
	return High;
}

int CRand::GetLow() 
{
	return Low;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?