本帖最後由 tonyh 於 2015-9-12 11:46 編輯
試產生4個範圍介於0~9, 不重複之隨機亂數.
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int r[4];
- srand(time(NULL));
- re:
- for(int i=0; i<4; i++)
- {
- r[i]=rand()%10; //0~9
- for(int j=0; j<i; j++)
- {
- if(r[j]==r[i])
- {
- i--;
- break;
- }
- }
- }
- for(int i=0; i<4; i++)
- cout<<r[i]<<" ";
- cout<<endl;
- system("pause");
- goto re;
- return 0;
- }
複製代碼 |