File: randomshuffle.cc

package info (click to toggle)
c%2B%2B-annotations 8.2.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,804 kB
  • ctags: 2,845
  • sloc: cpp: 15,418; makefile: 2,473; ansic: 165; perl: 90; sh: 29
file content (59 lines) | stat: -rw-r--r-- 1,587 bytes parent folder | download | duplicates (4)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <time.h>
    #include <iterator>
    using namespace std;

    int randomValue(int remaining)
    {
        return rand() % remaining;
    }
    class RandomGenerator
    {
        public:
            RandomGenerator()
            {
                srand(time(0));
            }
            int operator()(int remaining) const
            {
                return randomValue(remaining);
            }
    };
    void show(string *begin, string *end)
    {
        copy(begin, end, ostream_iterator<string>(cout, " "));
        cout << "\n\n";
    }
    int main()
    {
        string words[] =
                    { "kilo", "lima", "mike", "november", "oscar", "papa"};
        size_t const size = sizeof(words) / sizeof(string);

        cout << "Using Default Shuffle:\n";
        random_shuffle(words, words + size);
        show(words, words + size);

        cout << "Using RandomGenerator:\n";
        RandomGenerator rg;
        random_shuffle(words, words + size, rg);
        show(words, words + size);

        srand(time(0) << 1);
        cout << "Using the randomValue() function:\n";
        random_shuffle(words, words + size, randomValue);
        show(words, words + size);
    }
    /*
        Displays (for example):
            Using Default Shuffle:
            lima oscar mike november papa kilo

            Using RandomGenerator:
            kilo lima papa oscar mike november

            Using the randomValue() function:
            mike papa november kilo oscar lima
    */