// kadai1-1.cpp // ex) plot "???.txt" w l #define _USE_MATH_DEFINES #include <math.h> #include <stdio.h> #include <stdlib.h> #include <time.h> //ランダムウォークのステップ数 const int STEPS = 1000; //0以上1未満の一様乱数を生成する double frand() { return rand() / (RAND_MAX+1.0); } //Box-Muller法で標準正規乱数を生成する double box_muller() { //Box-Muller法は2個の一様乱数から2個の正規乱数を生成する //そのうち一方はすぐに返し、もう一方は次に呼び出されたときに返す static bool haveRandom = false; static double random; if(haveRandom){ //正規乱数はすでにできている haveRandom = false; return random; } else { //正規乱数は手元にないので新しく2個の正規乱数を作る double u1 = sqrt(-2*log(1-frand())); double u2 = 2*M_PI*frand(); //2個のうち一方は次の呼び出しのためにとっておく haveRandom = true; random = u1 * sin(u2); //もう一方を返す return u1 * cos(u2); } } int main() { double x, y; int step; //乱数を初期化する srand((unsigned int)time(NULL)); //STEPSステップのランダムウォークを実施する x = y = 0.0; for (step = 0; step < STEPS; step++) { x += box_muller(); y += box_muller(); printf("% 5.1f % 5.1f\n", x, y); } return 0; }
Standard input is empty
0.5 0.7 1.2 -0.4 1.1 -0.5 2.5 -0.6 1.6 -0.8 4.0 -1.6 2.7 -1.6 4.4 -1.8 4.1 -1.7 4.7 -3.0 5.6 -1.3 5.4 -1.6 5.4 -3.4 5.0 -1.7 3.5 -2.0 4.3 -2.1 5.1 -3.4 4.5 -5.2 3.3 -5.0 4.5 -5.9 4.4 -6.0 2.7 -6.8 2.3 -7.2 2.6 -6.0 2.7 -5.6 2.9 -5.8 3.7 -5.1 4.2 -3.9 2.6 -2.7 1.6 -3.2 1.6 -5.6 0.7 -6.5 -0.4 -5.8 0.3 -5.2 2.3 -5.0 2.4 -5.0 3.3 -6.2 3.8 -7.2 5.0 -7.2 4.7 -7.3 5.3 -7.0 4.3 -7.6 4.9 -7.2 6.5 -6.6 6.6 -5.5 5.3 -4.9 6.1 -2.0 5.8 -2.2 4.8 -1.2 3.3 -1.2 2.9 -2.1 3.8 -1.7 4.5 -2.8 6.6 -2.4 8.3 -3.3 9.5 -2.9 9.6 -2.8 10.1 -1.5 9.5 -1.2 9.1 -1.8 10.2 -2.8 10.0 -3.1 10.2 -2.8 10.1 -3.4 11.0 -4.2 11.6 -4.5 10.4 -5.2 12.6 -3.1 13.4 -3.9 13.3 -3.3 13.1 -5.1 13.4 -5.9 15.2 -6.3 18.6 -7.0 18.6 -6.0 19.9 -5.6 19.8 -6.1 20.0 -4.6 21.6 -5.7 19.9 -4.0 21.0 -2.6 20.5 -2.0 20.1 -0.4 20.8 0.5 20.4 0.1 21.4 -0.2 21.4 -1.1 22.2 0.4 22.3 -1.2 22.7 -0.9 22.8 -1.0 23.1 -0.6 23.7 -1.5 23.3 -0.6 22.5 0.8 21.0 0.4 22.1 0.4 22.9 2.5 22.2 2.9 21.3 3.2 21.4 2.9 20.4 3.4 21.0 4.4 21.3 5.7 21.2 3.4 22.0 3.1 20.8 3.3 21.3 3.3 20.4 3.3 21.2 3.7 23.1 3.9 22.7 5.2 22.5 5.2 22.2 6.3 23.3 4.1 24.9 4.1 25.7 5.2 26.6 5.7 27.5 5.8 27.4 6.7 26.2 5.5 26.1 6.2 25.9 6.6 25.3 7.1 23.6 6.3 22.9 5.4 24.1 6.0 22.3 4.6 21.1 5.3 19.0 4.6 18.6 4.0 19.2 2.9 20.9 2.6 22.5 4.2 23.2 7.2 23.2 6.2 21.7 5.0 21.9 4.9 21.5 7.2 21.5 6.3 21.9 4.4 21.0 4.8 20.2 6.6 19.9 6.7 21.4 6.1 21.6 6.1 20.4 5.6 18.9 6.6 18.6 5.4 17.4 5.2 19.5 4.1 19.5 3.3 19.1 4.5 17.6 4.8 18.9 4.7 18.3 5.0 16.9 4.1 15.5 2.6 17.3 1.3 17.1 1.0 18.5 2.2 17.2 2.7 15.9 3.4 16.4 5.3 16.0 5.7 17.5 6.3 18.3 6.4 19.3 4.9 18.4 4.9 19.1 4.5 18.0 4.4 18.0 5.3 18.1 5.7 20.2 5.9 20.6 5.9 22.0 5.3 21.9 5.8 22.0 4.8 22.1 6.3 23.5 3.9 23.3 2.5 25.2 1.6 24.8 -0.1 26.3 0.7 26.9 1.0 25.6 -1.7 25.0 -0.5 24.0 -2.6 24.5 -3.7 23.3 -4.7 22.3 -3.2 23.3 -4.0 24.7 -4.5 24.0 -3.1 23.6 -3.6 23.8 -4.2 24.8 -3.9 26.0 -2.9 24.3 -2.8 24.9 -3.0 25.5 -1.8 26.4 -0.8 25.3 -1.3 27.1 -1.0 26.1 -1.0 26.7 0.3 27.3 2.1 26.9 2.2 26.9 1.7 26.0 3.2 26.9 2.8 26.9 2.2 28.6 0.3 28.2 -1.6 29.1 -1.4 30.5 -2.1 30.2 -1.4 30.2 -1.7 31.5 -3.3 31.4 -1.6 30.6 -1.0 31.0 -0.0 29.7 -0.3 29.6 -1.7 28.9 -1.4 28.7 -1.7 28.9 -0.8 28.5 0.1 28.5 0.5 28.3 0.6 28.0 -0.2 27.1 -0.3 27.0 -0.2 27.6 -0.8 28.3 -0.7 27.9 -1.0 27.1 -1.6 28.9 -1.5 28.4 -1.4 27.1 -0.3 28.0 -2.2 28.0 -1.9 27.8 -1.8 28.8 -1.0 27.6 -1.9 28.8 -2.1 29.5 -2.7 29.0 -1.6 29.0 -2.4 28.4 -2.7 27.5 -1.3 27.7 -0.7 30.1 -0.6 30.7 -0.4 31.8 -0.7 31.1 0.1 29.4 0.3 29.8 -1.0 31.1 0.8 29.3 -0.3 27.7 0.3 28.9 0.7 29.8 0.9 29.2 0.2 27.6 -0.6 27.0 -0.8 25.1 0.4 26.1 -1.0 27.1 -1.2 28.6 -2.8 27.3 -1.0 27.0 -1.7 27.8 -3.4 27.8 -4.6 25.8 -2.9 25.6 -3.8 27.0 -5.0 27.8 -3.6 29.5 -1.9 29.3 -3.7 30.8 -3.5 30.2 -4.1 31.1 -3.7 31.7 -4.6 31.5 -4.5 32.9 -5.1 34.4 -5.8 33.1 -6.3 34.4 -5.3 34.1 -6.2 33.4 -7.1 33.0 -6.1 33.6 -5.1 32.4 -4.8 32.2 -4.0 32.9 -4.1 32.0 -4.1 31.9 -5.4 33.1 -4.9 32.0 -5.6 31.9 -4.6 32.6 -3.6 33.0 -3.1 34.4 -3.8 35.2 -3.7 35.0 -5.1 36.4 -4.7 36.6 -4.4 35.9 -4.6 34.4 -4.7 35.6 -6.2 36.3 -5.2 37.1 -4.6 38.9 -5.5 38.3 -5.3 37.4 -6.0 38.9 -5.6 40.1 -6.7 39.5 -6.5 37.9 -5.6 36.8 -5.3 37.2 -4.9 37.6 -3.8 37.3 -4.6 38.9 -5.0 38.3 -5.3 37.9 -7.1 38.6 -5.6 37.1 -6.7 37.6 -6.7 37.4 -6.9 37.2 -8.2 36.4 -7.0 37.2 -8.1 35.6 -7.4 35.6 -6.6 33.7 -5.6 33.0 -5.0 32.5 -6.3 32.3 -6.0 31.9 -4.6 29.5 -4.1 29.4 -4.2 30.1 -5.5 30.1 -6.3 29.5 -4.6 28.7 -4.0 28.9 -4.0 28.0 -5.3 25.9 -5.8 26.1 -5.4 27.9 -5.7 28.5 -4.6 28.7 -3.7 26.9 -2.8 25.6 -2.9 24.1 -3.3 23.5 -6.4 23.1 -7.0 21.8 -6.8 21.2 -6.0 21.5 -7.8 22.2 -9.3 20.4 -9.5 20.2 -10.6 19.9 -10.8 21.1 -12.4 19.9 -12.2 20.0 -12.4 21.2 -13.3 21.2 -12.5 22.2 -12.3 20.7 -11.4 22.3 -11.3 22.2 -11.0 21.1 -10.6 20.9 -9.2 20.3 -8.9 20.7 -8.7 18.5 -9.7 20.2 -9.7 20.0 -10.1 18.9 -11.8 19.3 -12.2 19.9 -13.3 20.8 -14.7 21.0 -14.3 22.5 -15.6 21.4 -16.4 20.0 -18.1 18.5 -16.6 18.1 -16.8 17.4 -15.5 17.3 -18.3 18.2 -18.7 18.6 -17.9 16.5 -17.1 16.8 -19.0 17.2 -19.0 16.2 -20.1 14.7 -19.1 15.6 -19.7 14.9 -18.6 15.7 -17.0 14.7 -19.1 15.6 -20.7 15.9 -20.9 15.7 -21.9 15.2 -21.3 14.4 -22.1 14.5 -22.9 14.9 -21.2 14.5 -21.7 13.7 -18.9 12.7 -20.0 10.5 -19.3 10.2 -19.2 9.6 -19.8 10.6 -18.6 9.7 -20.1 8.3 -21.3 9.1 -22.1 10.0 -22.9 10.1 -24.3 10.8 -24.1 9.8 -24.3 10.7 -22.7 12.3 -22.6 12.1 -24.4 11.3 -24.3 11.4 -23.3 9.6 -21.9 9.4 -20.5 9.3 -20.4 9.7 -21.4 9.7 -21.1 8.7 -21.3 9.1 -21.5 8.9 -22.5 8.0 -23.8 9.5 -23.0 8.9 -23.3 9.6 -23.5 10.8 -22.9 11.8 -24.0 13.0 -23.8 12.6 -23.6 12.5 -24.7 10.9 -24.1 11.2 -23.7 11.7 -23.2 11.3 -24.1 9.8 -24.3 11.1 -23.7 10.8 -23.0 12.4 -23.2 13.3 -23.6 12.5 -24.6 14.0 -23.3 12.5 -23.8 12.4 -21.0 13.6 -20.4 15.2 -20.3 13.4 -19.7 14.4 -20.3 14.0 -21.7 14.3 -21.6 13.9 -22.6 15.2 -22.6 15.4 -24.3 14.5 -24.2 14.0 -25.8 13.8 -24.8 13.5 -23.9 14.4 -23.6 13.0 -22.2 12.2 -21.9 13.3 -21.7 13.2 -20.6 14.1 -21.3 14.6 -20.9 14.9 -19.6 15.4 -19.6 14.1 -21.4 12.8 -24.2 12.5 -23.2 12.8 -22.6 12.7 -22.7 12.0 -21.8 13.4 -23.5 13.4 -24.6 13.3 -25.3 13.0 -25.9 11.9 -27.1 12.1 -24.5 12.8 -23.5 13.4 -22.8 12.5 -22.5 12.4 -22.0 12.7 -21.7 10.1 -21.9 10.7 -21.1 9.3 -20.4 10.4 -19.1 9.5 -20.6 10.7 -17.6 10.8 -16.6 9.0 -17.8 7.9 -19.8 8.4 -20.3 8.5 -22.4 8.5 -22.1 6.8 -21.0 8.3 -20.8 8.5 -22.3 8.3 -22.8 7.5 -23.0 9.3 -23.4 6.8 -24.6 6.8 -25.1 7.4 -24.6 5.9 -26.6 6.5 -27.3 6.5 -26.6 5.3 -26.3 4.8 -26.1 5.1 -25.5 6.2 -24.5 5.4 -23.4 5.3 -22.9 5.3 -22.3 3.2 -21.1 3.3 -23.0 3.6 -23.9 2.7 -23.9 1.5 -24.5 0.2 -24.6 0.8 -24.8 -0.1 -24.6 0.4 -25.0 -0.9 -23.0 -0.4 -22.2 0.8 -22.2 1.4 -21.7 0.6 -21.2 -0.3 -21.1 -1.1 -19.7 -3.1 -19.7 -3.2 -19.2 -3.9 -18.8 -2.9 -20.2 -5.1 -18.4 -4.8 -17.5 -4.9 -18.3 -6.3 -18.6 -7.5 -19.4 -5.5 -19.7 -6.8 -18.6 -4.1 -17.9 -4.7 -16.9 -3.8 -17.6 -2.6 -16.2 -2.9 -16.9 -3.4 -15.6 -1.2 -16.8 -1.3 -18.4 -1.8 -20.0 -1.4 -19.6 -3.5 -19.0 -5.3 -19.9 -8.1 -20.0 -7.7 -20.5 -6.7 -21.0 -7.4 -20.8 -7.6 -23.6 -9.2 -22.8 -10.4 -23.5 -9.6 -24.6 -9.4 -23.8 -9.3 -23.3 -8.9 -23.6 -7.0 -25.3 -5.9 -24.6 -6.1 -24.6 -5.1 -25.4 -5.4 -25.5 -5.3 -24.5 -4.6 -25.1 -5.0 -25.5 -4.4 -24.8 -3.7 -24.1 -2.5 -24.3 -1.4 -23.1 0.5 -23.4 1.4 -23.5 0.8 -23.2 1.7 -21.5 0.9 -21.1 0.5 -20.5 1.2 -22.6 2.0 -22.7 1.3 -23.3 1.1 -23.9 0.4 -23.0 1.0 -23.7 0.4 -24.7 3.2 -23.9 2.8 -25.3 2.2 -25.3 0.7 -25.1 1.5 -25.9 0.6 -23.8 0.6 -23.5 -0.2 -22.0 -0.8 -23.1 -0.6 -23.6 -2.0 -23.3 -1.4 -24.2 -0.8 -24.4 -1.5 -24.3 -2.3 -25.9 -2.2 -26.4 -1.7 -27.9 -0.9 -28.7 -0.5 -28.0 -0.6 -29.0 -0.6 -28.7 -0.4 -28.4 0.2 -29.5 0.9 -28.8 -0.1 -29.3 1.3 -27.3 1.1 -27.2 2.1 -24.3 2.5 -23.5 3.5 -22.4 4.4 -24.1 2.4 -24.9 1.3 -24.1 2.2 -24.6 2.6 -25.9 1.5 -26.8 1.4 -27.3 0.3 -28.4 0.8 -27.3 2.5 -26.9 2.0 -28.5 2.1 -29.1 2.2 -28.8 -0.1 -27.4 -0.6 -28.1 -0.3 -26.9 -1.4 -26.6 -0.0 -25.5 0.5 -23.7 0.4 -23.6 0.4 -23.6 0.5 -23.5 0.6 -24.5 2.0 -25.3 -0.3 -24.8 -1.5 -27.0 -1.1 -26.5 -1.1 -25.5 -1.9 -25.0 -2.6 -26.3 -2.5 -26.0 -4.4 -26.6 -5.3 -27.8 -6.8 -27.7 -7.7 -29.4 -6.4 -31.1 -5.6 -31.5 -6.2 -33.9 -7.0 -35.2 -8.1 -36.0 -8.5 -36.3 -8.8 -35.5 -9.1 -37.1 -7.9 -36.8 -7.8 -36.3 -7.3 -34.7 -8.2 -35.3 -6.7 -34.6 -7.3 -34.2 -6.3 -33.4 -6.6 -33.3 -8.7 -31.9 -7.9 -32.5 -7.4 -32.3 -7.5 -33.5 -8.0 -35.0 -7.1 -35.2 -4.2 -34.1 -5.7 -34.5 -5.5 -34.9 -4.8 -36.5 -4.8 -36.3 -4.7 -37.3 -5.1 -38.0 -4.8 -38.5 -4.7 -36.8 -4.9 -37.7 -5.4 -39.0 -5.3 -40.4 -4.5 -38.9 -5.6 -38.6 -4.6 -40.3 -3.9 -41.1 -2.1 -40.0 -1.1 -40.0 -0.0 -38.1 1.5 -39.1 1.7 -38.2 0.5 -39.1 1.6 -40.9 0.9 -41.7 0.9 -42.1 0.7 -43.0 1.8 -43.2 2.1 -42.1 2.6 -42.2 4.1 -42.1 5.8 -41.2 5.4 -43.2 5.3 -42.6 6.8 -42.0 5.8 -42.2 6.3 -44.8 5.2 -46.2 4.6 -47.0 5.1 -48.0 4.3 -48.3 3.6 -49.3 4.4 -49.4 4.6 -50.8 4.6 -51.4 5.6 -52.8 4.1 -53.7 2.6 -55.4 3.3 -54.9 5.4 -54.5 5.1 -54.0 6.0 -53.1 5.8 -52.7 4.1 -52.8 4.2 -50.0 4.1 -49.2 5.3 -50.5 7.1 -49.2 5.2 -49.5 7.2 -49.1 6.6 -49.7 8.6 -49.2 8.9 -49.9 7.4 -50.1 5.5 -51.1 6.7 -50.7 8.3 -49.2 8.9 -49.1 9.9 -49.5 8.8 -48.9 9.2 -49.4 10.7 -49.5 9.5 -50.5 9.1 -50.8 9.6 -50.9 8.4 -50.2 7.1 -50.1 7.5 -51.1 8.1 -50.5 8.5 -49.7 9.7 -50.6 9.1 -51.1 9.0 -51.0 7.6 -50.7 7.9 -49.8 5.8 -49.9 5.5 -50.1 5.8 -51.2 5.3 -51.5 4.9 -50.7 6.1 -50.7 7.2 -49.5 7.7 -48.4 8.2 -50.8 8.0 -50.0 10.7 -49.9 10.8 -49.3 11.3 -48.0 11.2 -46.7 12.5 -47.2 11.5 -47.5 10.4 -47.7 10.9 -47.8 10.5 -47.0 10.2 -48.2 11.5 -47.6 10.4 -46.4 10.7 -45.8 11.4 -44.8 13.0 -46.9 14.0 -47.3 12.6 -48.6 12.0 -48.5 12.1 -50.4 10.5 -50.1 11.1 -48.7 11.2 -48.5 12.2 -47.2 12.6 -47.2 11.8 -47.9 13.0 -46.9 11.7 -46.3 12.6 -47.0 13.9 -46.8 13.7 -47.0 11.7 -46.8 11.4 -48.0 11.2 -48.4 11.8 -48.7 12.7 -49.2 13.4 -48.5 13.0 -48.2 13.3 -49.0 12.4 -48.0 14.3 -47.6 11.6 -48.9 11.3 -49.4 8.7 -51.4 7.7 -52.2 6.7 -53.1 6.0 -53.1 5.9 -53.5 6.1 -52.9 5.3 -52.3 5.6 -52.7 4.7 -52.8 2.5 -51.6 2.7 -49.3 2.0 -49.3 3.2 -48.6 2.2 -49.4 1.6 -50.6 1.1 -51.3 1.5 -51.9 2.1 -52.4 1.1 -51.9 0.1 -52.2 -0.3 -52.5 -1.9 -52.9 -1.9 -52.9 -2.2 -52.1 -2.6 -51.2 -3.2 -49.6 -2.0 -50.0 -0.9 -48.4 -1.1 -48.2 -1.8 -49.7 -1.6 -49.7 -1.9 -48.9 -3.7 -49.7 -4.0 -49.0 -7.0 -51.5 -5.8 -50.0 -6.4 -50.4 -6.1 -48.9 -5.0 -48.6 -4.5 -49.7 -4.5 -48.8 -4.1 -49.1 -3.4 -49.3 -3.4 -49.0 -3.1 -49.8 -4.0 -47.6 -4.0 -45.7 -4.5 -47.0 -3.6 -48.6 -3.5 -48.7 -2.8 -49.6 -4.2 -51.7 -4.9 -52.6 -3.2 -53.7 -4.4 -52.8 -2.8 -52.5 -1.8 -53.5 -0.1 -55.1 1.7 -55.2 2.2 -54.8 3.8 -54.1 3.2 -54.9 4.1 -54.7 4.7 -54.0 3.4 -52.4 2.6 -50.0 3.2 -50.4 2.0 -52.7 2.7 -53.8 2.7 -52.4 2.4 -51.8 3.1 -52.2 3.2 -51.6 4.9 -51.6 5.6 -51.9 6.0 -51.8 2.6 -52.0 1.9 -50.6 3.1 -50.4 2.2 -49.7 1.4 -49.3 0.1 -50.2 -0.4 -51.0 0.3 -51.4 1.1 -52.3 1.1 -52.9 0.5 -52.7 -0.5 -51.7 -1.9 -51.1 -1.1 -51.6 -2.1 -51.4 -3.6 -49.6 -5.5 -51.5 -7.1 -51.5 -6.2 -50.4 -4.1 -50.0 -5.1 -50.8 -3.9 -51.5 -3.2 -51.9 -3.0 -53.8 -1.3 -53.9 -0.1 -54.3 0.4 -53.3 1.0 -52.7 0.3 -53.9 0.6 -54.0 1.1 -54.1 1.8 -53.5 3.2 -53.3 3.2 -51.9 2.6 -52.0 2.6 -52.8 3.0 -53.1 1.9 -52.1 0.1 -52.0 -0.5 -52.2 -0.9 -53.3 0.1 -53.8 0.8 -55.6 0.5 -55.5 1.4 -54.9 0.7 -55.7 0.7 -56.0 -0.1 -57.4 -1.1 -55.8 1.1 -55.9 2.0 -54.2 3.6 -54.4 3.3 -54.2 3.9 -54.9 1.9 -55.3 1.1 -54.6 1.7 -54.8 2.5 -54.5 1.4 -53.3 1.6 -52.0 2.7 -52.1 0.8 -52.5 -0.7 -53.2 -1.3 -53.5 0.6 -52.4 0.7 -53.7 0.4 -53.0 -0.4 -51.6 0.8 -50.6 0.3 -51.0 -0.6 -52.0 1.2 -54.5 0.8 -53.3 0.1 -51.2 -1.4 -50.8 -0.5 -49.9 -1.2 -49.1 -0.9 -47.7 0.3 -49.7 1.3 -51.2 -0.4 -49.7 0.2 -48.5 -0.8 -47.2 -1.9 -46.7 -2.1 -47.8 -1.9 -47.6 -1.0 -47.4 1.0 -48.7 1.6 -49.4 2.3 -49.4 2.2 -50.3 2.2 -50.8 2.3 -51.3 5.0 -50.6 5.2 -51.9