標題:
產生不重複之隨機亂數
[打印本頁]
作者:
李知易
時間:
2025-1-24 13:42
標題:
產生不重複之隨機亂數
本帖最後由 李知易 於 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;
}
複製代碼
作者:
陳婕愉
時間:
2025-1-25 17:36
此帖僅作者可見
作者:
陳頎諠
時間:
2025-2-1 12:06
此帖僅作者可見
作者:
江立辰
時間:
2025-2-1 14:29
此帖僅作者可見
作者:
吳孟樵
時間:
2025-2-1 19:25
此帖僅作者可見
作者:
彭郁程
時間:
2025-2-6 19:04
此帖僅作者可見
作者:
朱立人
時間:
2025-2-6 21:33
此帖僅作者可見
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2