SSブログ

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 << "回/秒";
}
─────

CPP random2.jpg


タグ:乱数
nice!(0)  コメント(0)  トラックバック(0) 

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

Facebook コメント

トラックバック 0

ペントミノ(pentomino.cpp、..detab.cpp ブログトップ

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。