00001 // $Id: mtrand.cpp 751 2006-03-31 15:43:49Z alex $ 00002 // mtrand.cpp : Defines the entry point for the console application. 00003 // 00004 /* @@tag:xara-cn-tp@@ THIRD PARTY COPYRIGHT */ 00005 #include "camtypes.h" 00006 #include "mtrand.h" 00007 00008 UINT32 MTRand::lastSeed = 1; 00009 00010 /******************************************************************************************** 00011 00012 > UINT32 MTRand::GetNextRandomNumberScaled(UINT32 MaxValue, UINT32 MinValue) 00013 00014 Author: Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com> 00015 Created: 04/11/2005 00016 Inputs: - 00017 Returns: the next number in an already seeded random number sequence, scaled to between 00018 the two specified values 00019 Purpose: as above, note that you MUST have already seeded the sequence 00020 SeeAlso: 00021 00022 ********************************************************************************************/ 00023 00024 UINT32 MTRand::GetNextRandomNumberScaled(UINT32 MaxValue, UINT32 MinValue) 00025 { 00026 UINT32 Random = this->operator ()(); 00027 double ScaleFactor = ((double)(MaxValue - MinValue)) / ((double)MTRAND_MAX); 00028 double Res = (double)Random * ScaleFactor; 00029 return (UINT32)Res + MinValue; 00030 } 00031 00032 00033 /******************************************************************************************** 00034 00035 > UINT32 MTRand::GetNextRandomNumberScaled(UINT32 MaxValue, UINT32 MinValue, UINT32 Median) 00036 00037 Author: Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com> 00038 Created: 04/11/2005 00039 Inputs: MaxValue - the top of our random range 00040 MinValue - the bottom of our random range 00041 Median - duh 00042 Returns: the next number in an already seeded random number sequence, scaled to between 00043 the two specified values 00044 Purpose: Overridden version which allows you to have a non-linear range. By specifying the 00045 median all random values > MTRAND / 2 will be scaled from Median - MaxValue and 00046 all random values <= MTRAND / 2 will be scaled from MinValue - Median. 00047 SeeAlso: 00048 00049 ********************************************************************************************/ 00050 00051 UINT32 MTRand::GetNextRandomNumberScaled(UINT32 MaxValue, UINT32 MinValue, UINT32 Median) 00052 { 00053 UINT32 Random = rand(); 00054 00055 UINT32 Retval = 0; 00056 if (Random < MTRAND_MED) 00057 { 00058 double ScaleFactor = ((double)(Median - MinValue)) / (double)MTRAND_MED; 00059 double Res = ScaleFactor * Random; 00060 Retval = (UINT32)Res + MinValue; 00061 } 00062 if (Random >= MTRAND_MED) 00063 { 00064 double ScaleFactor = ((double)(MaxValue - Median)) / (double)MTRAND_MED; 00065 double Res = ScaleFactor * (Random - MTRAND_MED); 00066 Retval = (UINT32)Res + Median; 00067 } 00068 return Retval; 00069 }