random2.cpp [C++]
// Random
//
// 2012.03.03 coded by 心如
#include <iostream>
// #include <ctime>
using namespace std;
// 乱数(0~32767の整数)
class Random{
unsigned long nxt;
public:
Random(){ nxt = 1; }
Random(int sv){ nxt = (unsigned long) sv; }
int GetV(){
nxt = nxt * 1103515245 + 12345;
return (int)((nxt / 65536) % 32768);
}
};
// 経過時間
class Timer{
clock_t stime;
public:
Timer(){ stime = clock(); }
float GetTime(){
return (float )(clock() - stime) / CLOCKS_PER_SEC;
}
};
const Mxn = 200;
const Kai = 1000000;
int main(){
int i, n, max, min;
float fv;
int da[Mxn] = { 0};
Random a;
Timer st;
for( i = 0; i < Mxn * Kai; i++){
da[ a.GetV() % Mxn] += 1;
}
max = Kai;
min = Kai;
for(i = 0; i < Mxn; i++){
printf("%7d,", n = da[ i]);
if( max < n){
max = n;
}
else if( min > n){
min = n;
}
}
cout << endl << "Max=" << max << " Min="<< min << endl;
fv = st.GetTime();
cout << fv << "秒 "<< Mxn * Kai / fv << "回/秒";
}
─────
コメント 0