내 의도는 반복없이 1 ~ 9 난수를 생성하는 것입니다C++ 반복없이 임의의 숫자를 생성. 출력 화면에는 깜박이는 커서가 비어 있습니다.
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int randrange(int low,int high) /* generates a random number within given range*/
{
return rand()%(low+high)+low+1;
}
int main()
{
int num[9]={0},i,j;
bool check;
for(i=0;i<9;i++)
{
check=false;
do
{
num[i]=randrange(1,9);
for(j=0;j<i;j++)
{
if(num[i]==num[j]) // checks whether number already exists in the array
check=false;
else
check=true;
}
} while(check==false);
}
// the program is working fine without the repetition check
// this section prints out the array elements
for(i=0;i<9;i++)
{
cout<<num[i]<<" ";
}
return 0;
}
을 더 나은 알고리즘, 그것은 문제가 무엇인지, 왜 프로그램이 멈추는 지 OP 질문에 대답하지 않습니다. –