C++产生固定范围内的固定数量的随机数

 1 #include<iostream>
 2 #include<ctime>
 3 #include<random>
 4 using namespace std;
 5 void knuth(int n, int m)
 6 {
 7     srand((unsigned int)time(NULL));
 8     for (int i = 0; i < n; i++) {
 9         /*  注意到在整个for循环中,随机数种子已经固定,rand()
10             的值是不变的
11             这里n必须减去i,否则很有可能产生的随机数量小于n
12         */ 
13         if (rand() % (n - i) < m ) {
14             cout << i << endl;
15             --m;
16         }
17      }
18 }
19 
20 int main()
21 {
22     knuth(1000, 10);
23     return 0;
24 }

上述程序是假设m远远大于n!

原文地址:https://www.cnblogs.com/90zeng/p/cpp_rand1.html