File: pcg-demo.cpp

package info (click to toggle)
pcg-cpp 0.98.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 624 kB
  • sloc: cpp: 2,752; makefile: 201; sh: 59
file content (194 lines) | stat: -rw-r--r-- 6,505 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * PCG Random Number Generation for C++
 *
 * Copyright 2014 Melissa O'Neill <oneill@pcg-random.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * For additional information about the PCG random number generation scheme,
 * including its license and other licensing options, visit
 *
 *     http://www.pcg-random.org
 */

/*
 * This file is based on the demo program for the C generation schemes.
 * It shows some basic generation tasks.
 */

#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <random>       // for random_device

#include "pcg_random.hpp"

// This code can be compiled with the preprocessor symbol RNG set to the
// PCG generator you'd like to test.

#ifndef RNG
    #define RNG pcg32
    #define TWO_ARG_INIT 1
#endif

#define STRINGIFY_IMPL(x) #x
#define STRINGIFY(x)      STRINGIFY_IMPL(x)

using namespace std;
using pcg_extras::operator<<;   // Cause uint8_t to be printed as an int.

int main(int argc, char** argv)
{
    // Read command-line options

    int rounds = 5;
    bool nondeterministic_seed = false;

    ++argv;
    --argc;
    if (argc > 0 && strcmp(argv[0], "-r") == 0) {
        nondeterministic_seed = true;
        ++argv;
        --argc;
    }
    if (argc > 0) {
        rounds = atoi(argv[0]);
    }

    /* Many of the generators can be initialized with two arguments; the second
     * one specifies the stream.
     */

#if TWO_ARG_INIT
    RNG rng(42u, 54u);
#else
    RNG rng(42u);
#endif

    if (nondeterministic_seed) {
        // Seed with external entropy from std::random_device (a handy
        // utility provided by pcg_extras).
        rng.seed(pcg_extras::seed_seq_from<std::random_device>());
    }

    constexpr auto bits = sizeof(RNG::result_type) * CHAR_BIT;
    constexpr int how_many_nums = bits <= 8  ? 14
                                : bits <= 16 ? 10
                                :              6;
    constexpr int wrap_nums_at  = bits >  64 ? 2
                                : bits >  32 ? 3
                                :              how_many_nums;

    cout << STRINGIFY(RNG) << ":\n"
    //   << "      -  aka:         " << pcg_extras::printable_typename<RNG>()
    // ^-- we skip this line because the output is long, scary, ugly, and
    //     and varies depending on the platform
         << "      -  result:      " <<  bits << "-bit unsigned int\n"
         << "      -  period:      2^" << RNG::period_pow2();
    if (RNG::streams_pow2() > 0)
         cout << "   (* 2^" << RNG::streams_pow2() << " streams)";
    cout << "\n      -  size:        " << sizeof(RNG) << " bytes\n\n";

    for (int round = 1; round <= rounds; ++round) {
        printf("Round %d:\n", round);

        /* Make some N-bit numbers */
        cout << setw(4) << setfill(' ') << bits << "bit:";
        for (int i = 0; i < how_many_nums; ++i) {
            if (i > 0 && i % wrap_nums_at == 0)
                cout << "\n\t";
            cout << " 0x" << hex << setfill('0') 
                 << setw(sizeof(RNG::result_type)*2) << rng();
        }
        cout << endl;

        cout << "  Again:";
        rng.backstep(6);
        for (int i = 0; i < how_many_nums; ++i) {
            if (i > 0 && i % wrap_nums_at == 0)
                cout << "\n\t";
            cout << " 0x" << hex << setfill('0') 
                 << setw(sizeof(RNG::result_type)*2) << rng();
        }
        cout << dec << endl;

        /* Toss some coins */
        cout << "  Coins: ";
        for (int i = 0; i < 65; ++i)
            cout << (rng(2) ? "H" : "T");
        cout << endl;

        RNG rng_copy{rng};
        /* Roll some dice */
        printf("  Rolls:");
        for (int i = 0; i < 33; ++i)
            cout << " " << (uint32_t(rng(6)) + 1);
        cout << "\n   -->   rolling dice used " 
             << (rng - rng_copy) << " random numbers" << endl;

        /* Deal some cards using std::shuffle 
         * It's unspecified *how* std::shuffle shuffles the cards, or how many
         * random numbers it will use to do so, so we call std::shuffle and
         * measure how good it is.  We won't use it for the final shuffle
         * to avoid platform-dependent output.
         */
        rng_copy = rng;
        enum { SUITS = 4, NUMBERS = 13, CARDS = 52 };
        char cards[CARDS];
        iota(begin(cards), end(cards), 0);
        std::shuffle(begin(cards), end(cards), rng);
        auto std_shuffle_steps = rng - rng_copy;
        
        /* Restore RNG and deal again using pcg_extras::shuffle, which follows
         * the algorithm for shuffling that most programmers would expect.
         */      
        rng = rng_copy;
        iota(begin(cards), end(cards), 0);
        pcg_extras::shuffle(begin(cards), end(cards), rng);
        auto my_shuffle_steps = rng - rng_copy;

        /* Output the shuffled deck */
        printf("  Cards:");
        static const signed char number[] = {'A', '2', '3', '4', '5', '6', '7',
                                             '8', '9', 'T', 'J', 'Q', 'K'};
        static const signed char suit[] =   {'h', 'c', 'd', 's'};
        int i = 0;
        for (auto card : cards) {
            ++i;
            cout << " " << number[card / SUITS] << suit[card % SUITS];
            if (i % 22 == 0)
                cout << "\n\t";
        }
        
        /* Output statistics about shuffling */
        cout <<   "\n   -->   std::shuffle used "
             << std_shuffle_steps << " random numbers";
        typedef RNG::state_type rngdelta_t;
        if (std_shuffle_steps > rngdelta_t(52)) {
            cout << "\n\t -- that's " << (std_shuffle_steps - rngdelta_t(51)) 
                 << " more than we'd expect; inefficient implementation";
        }
        cout <<   "\n   -->   pcg_extras::shuffle used "
             << my_shuffle_steps << " random numbers";

        cout << "\n" << endl;
    }

    return 0;
}