本帖最後由 李知易 於 2025-1-24 21:10 編輯
試產生20組4個範圍介於0~9, 不重複之隨機亂數.

本帖隱藏的內容需要回復才可以瀏覽 - #include<iostream>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- int main()
- {
- srand(time(NULL));
- cout<<"4個介於0~9的不重複隨機亂數:"<<endl;
- for(int i = 0; i < 20; i++)
- {
- int n[10] = {0};
- for(int j = 0; j < 4; j++)
- {
- int r = rand()%10;
- while(n[r]==1)
- {
- r = rand()%10;
- }
- cout<<r<<" ";
- n[r]++;
- }
- cout<<endl;
- }
- return 0;
- }
複製代碼 |