Skip to content

Instantly share code, notes, and snippets.

@alphaKAI
Created December 3, 2012 07:42
Show Gist options
  • Save alphaKAI/4193429 to your computer and use it in GitHub Desktop.
Save alphaKAI/4193429 to your computer and use it in GitHub Desktop.
こんな感じ(動作確認用のダミーmain関数入り)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM 10
int GetRandom(int const max);
/* 動作テスト用のダミー */
int main(void){
printf("return Result:%d", GetRandom(NUM));
return 0;
}
/* 関数本体 */
int GetRandom(int const max){
int num, i;
int k=0;
int sum[10];
int result;
srand((unsigned)time(NULL));
while(k!=1){
for(i=1; i<9; i++){
sum[i] = rand() %max+1;
}
num = rand() % 9 + 1;
result=sum[num];
if(max < result){
printf("Error\n");
printf("理由:\n生成された乱数の返り値の変数resultの値が\n乱数生成の最大値の引数 max変数より大きかったため値が異常です\n", num);
printf("参考用:%d\n", result);
printf("再試行します\n\n");
result = 0;
num = 0;
for(i=1; i<9; i++){
sum[i] = 0;
}
}
else{
printf("乱数は正常に生成されました\n");
k = 1;
}
}
printf("Result:%d\n",result);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment