- UID
- 10108
- 积分
- 5956
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-9-17
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
cRandom.h
- #ifndef CRANDOM_H
- #define CRANDOM_H
- #include<iostream>
- using namespace std;
- #pragma once
- #define N 624
- #define M 397
- #define NUM 100000
- #define MATRIX_A 0x9908b0df
- #define UPPER_MASK 0x80000000
- #define LOWER_MASK 0x7fffffff
- #define MTRand_Max 0xffffffff
- #define TEMPERING_MASK_B 0x9d2c5680
- #define TEMPERING_MASK_C 0xefc60000
- #define TEMPERING_SHIFT_U(ulMidValue) (ulMidValue >> 11)
- #define TEMPERING_SHIFT_S(ulMidValue) (ulMidValue << 7)
- #define TEMPERING_SHIFT_T(ulMidValue) (ulMidValue << 15)
- #define TEMPERING_SHIFT_L(ulMidValue) (ulMidValue >> 18)
- class cRandom
- {
- public:
- bool RandomUniform(double *t_pdArray,unsigned long t_ulNumber,double t_dUp,double t_dDown);
- bool RandomGaussion(double *t_pdArray,unsigned long t_ulNumber,double t_dMean,double t_dStD);
- bool RandomCircle(double *t_pdXArray,double *t_pdYArray,unsigned long t_ulNumber,double t_dXcenter,double t_dYcenter,double t_dRadius);
- bool RandomSequence(double *t_pdArray,unsigned long t_ulNumber);
- cRandom()
- {
- wSeedFlag = 0;
- bInitFlag = 0;
- }
- ~cRandom()
- {
- //cout << "Destructor called" << endl;
- }
- private:
- unsigned short wSeedFlag;
- bool bInitFlag;
- unsigned long ulaSeed[N];
- void MTRandom (unsigned long *t_ulpRandom,unsigned long t_ulNumber);
- };
- #endif // CRANDOM_H
cRandom.cpp
- #include "cRandom.h"
- #include<ctime>
- #include <fstream>
- #include<cstdlib>
- #include<cmath>
- #include<iostream>
- #include<conio.h>
- #include<string>
- using std::ifstream;
- using std::ofstream;
- using std::cin;
- using std::cout;
- using std::endl;
- void cRandom::MTRandom(unsigned long *t_ulpRandom,unsigned long t_ulNumber)
- {
- unsigned long ulMidValue;
- unsigned short wTemp2,wMTNum = N,wPostValue;
- unsigned long ulMag[2] =
- {
- 0x0,MATRIX_A
- };
- if(bInitFlag==0)
- {
- unsigned long ulInitSeed=unsigned long(time(0));
- ulaSeed[0]= ulInitSeed & 0xffffffffUL;
- for (unsigned short wtemp1 = 1; wtemp1 < N; wtemp1++)
- {
- ulaSeed[wtemp1] = (1812433253UL * (ulaSeed[wtemp1-1] ^ (ulaSeed[wtemp1-1] >> 30)) + wtemp1);
- ulaSeed[wtemp1] &= 0xffffffffUL;
- }
- bInitFlag = 1;
- }
- wPostValue = (t_ulNumber + wSeedFlag - 1) % N + 1;
- for (unsigned long ulTemp = 0; ulTemp < t_ulNumber; ulTemp++)
- {
- if((wSeedFlag != 624) && (wSeedFlag != 0))
- {
- t_ulpRandom[ulTemp] = ulaSeed[wSeedFlag];
- wSeedFlag ++;
- }
- else
- {
- if(wMTNum >= N)
- {
- for (wTemp2 = 0; wTemp2 < N-M; wTemp2 ++)
- {
- ulMidValue = (ulaSeed[wTemp2] & UPPER_MASK) | (ulaSeed[wTemp2 + 1] & LOWER_MASK);
- ulaSeed[wTemp2] = ulaSeed[wTemp2 + M] ^ (ulMidValue >> 1)^ ulMag[ulMidValue & 0x1];
- }
- for (; wTemp2 < N-1; wTemp2++)
- {
- ulMidValue = (ulaSeed[wTemp2] & UPPER_MASK) | (ulaSeed[wTemp2 + 1] & LOWER_MASK);
- ulaSeed[wTemp2] = ulaSeed[wTemp2 + (M - N)] ^ (ulMidValue >> 1)^ ulMag[ulMidValue & 0x1];
- }
- ulMidValue = (ulaSeed[N - 1] & UPPER_MASK) | (ulaSeed[0] & LOWER_MASK);
- ulaSeed[N - 1] = ulaSeed[M - 1] ^ (ulMidValue >> 1) ^ ulMag[ulMidValue & 0x1];
- wMTNum = 0;
- }
- ulMidValue = ulaSeed[wMTNum++];
- ulMidValue ^= TEMPERING_SHIFT_U(ulMidValue);
- ulMidValue ^= TEMPERING_SHIFT_S(ulMidValue) & TEMPERING_MASK_B;
- ulMidValue ^= TEMPERING_SHIFT_T(ulMidValue) & TEMPERING_MASK_C;
- ulMidValue ^= TEMPERING_SHIFT_L(ulMidValue);
- t_ulpRandom[ulTemp] = ulMidValue;
- }
- }
- wSeedFlag = wPostValue;
- }
- bool cRandom::RandomUniform(double *t_pdArray,unsigned long t_ulNumber,double t_dUp,double t_dDown)
- {
- bool bState;
- double dInterval = (t_dUp - t_dDown) / MTRand_Max;
- unsigned long *ulpNumber;
- ulpNumber = new unsigned long[t_ulNumber];
- memset(ulpNumber,0,sizeof(ulpNumber));
- MTRandom(ulpNumber,t_ulNumber);
- if(t_ulNumber >= 0)
- {
- for(unsigned long ulTemp=0; ulTemp<t_ulNumber; ulTemp ++)
- {
- t_pdArray[ulTemp] = double(ulpNumber[ulTemp]) * dInterval + t_dDown;
- }
- bState = true;
- }
- else
- {
- bState = false;
- }
- delete []ulpNumber;
- return bState;
- }
- bool cRandom::RandomCircle(double *t_pdXArray,double *t_pdYArray,unsigned long t_ulNumber,double t_dXcenter,double t_dYcenter,double t_dRadius)
- {
- bool bState;
- unsigned long *ulpNumber;
- ulpNumber=new unsigned long[t_ulNumber << 1];
- memset(ulpNumber,0,t_ulNumber * sizeof(unsigned long) << 1);
- MTRandom(ulpNumber,t_ulNumber << 1);
- double dRadius = 0, dRandom1, dRandom2,dMiddle;
- unsigned long ulTemp = 0;
- dMiddle = (t_dRadius * 2) / MTRand_Max;//<<只试用于整型的数
- if(t_ulNumber > 0)
- {
- for(unsigned long ultemp = 0; (ultemp != t_ulNumber - 1)&&(ulTemp != t_ulNumber); ultemp++)
- {
- dRandom1 = ulpNumber[(ultemp << 1)] * dMiddle - t_dRadius;//调用MT的次数太多了,进行修改
- dRandom2 = ulpNumber[(ultemp << 1) + 1] * dMiddle - t_dRadius;//调用MT的次数太多了,进行修改
- dRadius = sqrt(dRandom1 * dRandom1 + dRandom2 * dRandom2);
- if (dRadius <= t_dRadius)
- {
- t_pdXArray[ulTemp] = dRandom1 + t_dXcenter;
- t_pdYArray[ulTemp] = dRandom2 + t_dYcenter;
- ulTemp ++;
- }
- if(ultemp==t_ulNumber)
- {
- ultemp=0;
- MTRandom(ulpNumber,t_ulNumber<<2);
- }
- }
- bState = true;
- }
- else
- {
- bState = false;
- }
- delete []ulpNumber;
- return bState;
- }
- bool cRandom::RandomGaussion(double *t_pdArray,unsigned long t_ulNumber,double t_dMean,double t_dStD)
- {
- bool bState;
- unsigned long ulTemp,ulNum;
- double dRandomx,dRandomy,dRadius,dAngle;
- unsigned long *ulpNumber;
- ulNum = t_ulNumber + (t_ulNumber % 2);
- ulpNumber = new unsigned long[ulNum];
- memset(ulpNumber,0,ulNum*sizeof(unsigned long));
- MTRandom(ulpNumber,ulNum);
- ulNum = t_ulNumber >> 1;
- if (t_dStD >= 0)
- {
- for(ulTemp = 0 ; ulTemp < ulNum ; ulTemp++)
- {
- dRandomx = (double)ulpNumber[ulTemp << 1] / MTRand_Max;
- dRandomy = (double)ulpNumber[(ulTemp << 1) + 1] / MTRand_Max;
- dRadius = (double)sqrt((-2) * log(dRandomx));
- dAngle = 2 * 3.1415926 * dRandomy;
- t_pdArray[ulTemp << 1] = (dRadius * cos(dAngle)) * t_dStD + t_dMean;
- t_pdArray[(ulTemp << 1) + 1] = (dRadius * sin(dAngle)) * t_dStD + t_dMean;
- }
- if (t_ulNumber % 2 == 1)
- {
- dRandomx = (double)ulpNumber[ulTemp] / MTRand_Max;
- dRandomy = (double)ulpNumber[ulTemp + 1] / MTRand_Max;
- dRadius = (double)sqrt((-2) * log(dRandomx));
- dAngle = 2 * 3.1415926 * dRandomy;
- t_pdArray[ulTemp << 1] = (dRadius * cos(dAngle)) * t_dStD + t_dMean;
- }
- bState = true;
- }
- else
- {
- bState = false;
- }
- delete []ulpNumber;
- return bState;
- }
- bool cRandom::RandomSequence(double *t_pdArray,unsigned long t_ulNumber)
- {
- bool bState;
- unsigned long *ulpNumber,ulMiddle,ulTemp,ulNum,ulIndex;
- ulpNumber = new unsigned long[t_ulNumber * 18];
- memset(ulpNumber,0,t_ulNumber * 18*sizeof(unsigned long));
- MTRandom(ulpNumber,t_ulNumber * 18);
- if(t_ulNumber>0)
- {
- for(ulNum=0; ulNum<18; ulNum++)
- {
- for(ulTemp=0; ulTemp<t_ulNumber; ulTemp++)
- {
- ulIndex=ulpNumber[ulNum*18+ulTemp]%t_ulNumber;
- ulMiddle=t_pdArray[ulTemp];
- t_pdArray[ulTemp]=t_pdArray[ulIndex];
- t_pdArray[ulIndex]=ulMiddle;
- }
- }
- bState=true;
- }
- else
- {
- bState = false;
- }
- delete []ulpNumber;
- return bState;
- }
测试程序 main.cpp
- #include <iostream>
- #include "cRandom.h"
- #include <ctime>
- #include <fstream>
- #include <cstdlib>
- #include <cmath>
- #include <conio.h>
- #include <string.h>
- using std::ifstream;
- using std::ofstream;
- using std::cin;
- using std::cout;
- using std::endl;
- using namespace std;
- int main()
- {
- cRandom cDistribute;
- unsigned short chFlag;
- unsigned long ulNumber, ulTemp;
- double *pdArray, *pdXArray, *pdYArray;
- bool bResult;
- cout << "菜单选择" <<"\n"
- << "1.均匀分布" <<"\n"
- << "2.高斯分布" <<"\n"
- << "3.二维均匀随机分布" <<"\n"
- << "4.随机序列排序" <<"\n"
- << "5.退出"<< endl;
- while(1)
- {
- cin >> chFlag ;
- if (chFlag == 1)
- {
- double dUp,dDown;
- cout << "请输入产生均匀分布个数:" << endl;
- cin >> ulNumber;
- pdArray = new double[ulNumber];
- memset(pdArray,0,ulNumber*sizeof(double));
- cout << "请输入上限和下限:" << endl;
- cin >> dUp >> dDown;
- bResult = cDistribute.RandomUniform (pdArray, ulNumber,dUp,dDown);
- if (bResult == true)
- {
- cout << "均匀分布生成成功" << endl;
- }
- else
- {
- cout << "均匀分布生成失败" << endl;
- return 0;
- }
- ofstream fFile("RandomUniform.txt");
- for (ulTemp = 0; ulTemp < ulNumber; ulTemp ++)
- {
- fFile << pdArray[ulTemp] << endl;
- }
- fFile.close();
- fFile.clear();
- delete []pdArray;
- }
- else if (chFlag == 2)
- {
- double dMean,dStD;
- cout << "请输入产生高斯分布个数:" << endl;
- cin >> ulNumber;
- pdArray = new double[ulNumber];
- memset(pdArray,0,ulNumber * sizeof(double));
- cout << "请输入均值和方差" << endl;
- cin >> dMean >> dStD;
- bResult = cDistribute.RandomGaussion(pdArray,ulNumber,dMean,dStD);
- if (bResult == true)
- {
- cout << "高斯分布生成成功" << endl;
- }
- else
- {
- cout << "高斯分布生成失败" << endl;
- return 0;
- }
- ofstream fFile("RandomUGaussion.txt");
- for (ulTemp = 0; ulTemp < ulNumber; ulTemp ++)
- {
- fFile << pdArray[ulTemp] << endl;
- }
- fFile.close();
- fFile.clear();
- delete []pdArray;
- }
- else if (chFlag == 3)
- {
- double dXCenter,dYCenter,dRadius;//the center's location
- cout << "请输入产生圆上坐标的对数:" << endl;
- cin >> ulNumber;
- pdXArray = new double[ulNumber];
- pdYArray = new double[ulNumber];
- memset(pdXArray,0,ulNumber * sizeof(double));
- memset(pdYArray,0,ulNumber * sizeof(double));
- cout << "输入圆心的横纵坐标" << endl;
- cin >> dXCenter >> dYCenter;
- cout << "输入圆的半径" << endl;
- cin >> dRadius;
- bResult = cDistribute.RandomCircle(pdXArray,pdYArray,ulNumber,dXCenter,dYCenter,dRadius);
- if (bResult == true)
- {
- cout << "几何分布生成成功" << endl;
- }
- else
- {
- cout << "几何分布生成失败" << endl;
- return 0;
- }
- ofstream fFile("RandomCircle.txt");
- for (ulTemp = 0; ulTemp < ulNumber; ulTemp ++)
- {
- fFile << pdXArray[ulTemp] << '\t' << pdYArray[ulTemp]<< endl;
- }
- fFile.close();
- fFile.clear();
- delete []pdXArray;
- delete []pdYArray;
- }
- else if (chFlag == 4)
- {
- ofstream fFile("NumberList.txt",ios::out);
- cout << "请输入随机数个数:" << endl;
- cin >> ulNumber;
- pdArray = new double[ulNumber];
- memset(pdArray,0,ulNumber*sizeof(double));
- cout << "请输入序列:" << endl;
- for(ulTemp = 0; ulTemp < ulNumber; ulTemp++)
- {
- cin >> pdArray[ulTemp];
- }
- bResult=cDistribute.RandomSequence(pdArray,ulNumber);
- if (bResult == true)
- {
- cout << "随机序列生成成功" << endl;
- }
- else
- {
- cout << "随机序列生成失败" << endl;
- return 0;
- }
- for(ulTemp = 0; ulTemp < ulNumber; ulTemp++)
- {
- fFile << pdArray[ulTemp] << endl;
- }
- fFile.close();
- fFile.clear();
- delete []pdArray;
- }
- else if (chFlag == 5)
- {
- return 0;
- }
- else
- {
- cout << "error!" << endl;
- }
- }
- }
|
|