  
|
產生介於指定範圍內的隨機亂數 (一)
rand()%x -> 0~(x-1)(int)
試產生20組範圍介於0~2的隨機亂數。- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- int main()
- {
- srand(time(NULL));
- for(int i=1; i<=20; i++)
- cout<<rand()%3<<endl;
- system("pause");
- return 0;
- }
複製代碼 |
|