Skip to content

Instantly share code, notes, and snippets.

Created June 6, 2017 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/444abd70ec4edec72d57ab7a801329f0 to your computer and use it in GitHub Desktop.
Save anonymous/444abd70ec4edec72d57ab7a801329f0 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include <time.h>
#include <stdlib.h>
int main(void) {
// 乱数の初期設定(これにより、毎回違う乱数が生成される)
srand((unsigned) time(NULL));
// 各出現数字をカウント。{0}とすると、全データ0で初期化できるよ
int hist[10] = {0};
// 同値カウント, 生成乱数のカウント
int same_cnt = 0, rand_cnt = 0;
// 乱数格納変数, 1回前の乱数
int now_num = -1, old_num;
int i; // ループカウンタ
// 同じ数が3回連続出るまでループ
while ( same_cnt < 3 && ++rand_cnt ) {
// 前回の乱数、今回の乱数の更新
old_num = now_num;
now_num = rand() % 10;
// 今回の出現値をカウント
hist[now_num]++;
// 今回と前回が同値ならカウントUp。そうでなければリセット
same_cnt = (old_num == now_num) ? same_cnt + 1 : 0;
}
// 出力するよー
printf("発生した乱数の総数:%d\n\n", rand_cnt);
for(i = 0; i < 10; i++) printf("%d の出現回数 -> %d\n", i, hist[i]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment