先日 、乱数を学び、試しに何か作ってみようと思い、
3つの重複しない10以下の整数を出すプログラムを作ってみたのですが、
数が重複してしまったり、3つ目の数が1に固定されていたりと挙動がおかしいです
修正すべき点を教えていただけると嬉しいです
ちなみに、環境はiPhoneでideoneのサイトで行っています
10回動かしてみた結果は、651、641、211、481、701、541、341、121、491、461
となりました
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
printf("コードブレイカー 3桁の数字を入力\n");
srand(time(0));
int ans[3] = {0};
ans[0] = rand() % 10;
while(1)
{
ans[1] = rand() % 10;
if (ans[1] != ans[0])
break;
}
while(1)
{
ans[2] = rand() % 10;
if (ans[2] != ans[0] && ans[2]!= ans[1])
break;
}
int i;
for(i=1;i<=3;i++)
{
printf("%d\n",ans[i]);
}
return 0;
}