1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 
제너릭 random_shuffle 알고리즘은 모의 난수(pseudo-random number)를 발생키는 함수를
이용하여 구간 [first, last) 내의 원소들을 무작위로 뒤섞는다. random_shuffle이 만들어
내는 순열들은 거의 균등한 분포를 이룬다. 즉 사이즈 N인 구간에 속하는 원소들로 만들
수 있는 N! 개 순열들 각각의 확률은 거의 1/N!에 가깝다.
 
random_shuffle의 시간 복잡도는 선형적이다.
 
*/
 
#include <algorithm>
#include <vector>
#include <functional>
#include <iostream>
 
using namespace std;
 
int main()
{
    cout<< "Illustrating the random_shuffle algorithm." << endl;
    const int N = 20;
    vector<int> vector1(N);
    for (int i=0; i< N; ++i)
        vector1[i] = i;
 
    for (int j=0; j< 3; ++j)
    {
        // vector1에 담긴 정수들을 무작위로 뒤섞는다.
        random_shuffle(vector1.begin(), vector1.end());
 
        // vector1의 내용을 출력
        copy(vector1.begin(), vector1.end(), ostream_iterator<int>(cout, " "));
        cout<< endl;
    }
 
    return 0;
}

'STL > 변경 가능 시퀀스 알고리즘' 카테고리의 다른 글

[STL] replace  (0) 2010.05.17
[STL] remove  (0) 2010.05.17
[STL] partition  (0) 2010.05.17
[STL] generate  (0) 2010.05.17
[STL] fill  (0) 2010.05.17