1.生成[0,n)的之间k个不重复的随机整数。 http://www.fookwood.com/archives/605
1 | #include<iostream> |
2 | #include<cstdlib> |
3 | #include<algorithm> |
4 | #include<cstdio> |
5 | using namespace std; |
6 | const int N = 10000000; |
7 | const int K = 10000000; |
8 | int randint( int l, int r ){ |
9 | return rand () % ( r-l ) + l; |
10 | } |
11 | int a[ N ]; |
12 | int main( void ){ |
13 | for ( int i = 0; i < N; i++ ) |
14 | a[i] = i; |
15 | for ( int i = 0; i < K; i++ ){ |
16 | swap( a[i], a[ randint(i,N) ] ); |
17 | printf ( "%d\n" ,a[i]); |
18 | } |
19 | return 0; |
20 | } |