1 /*
2  * PCG Random Number Generation for C.
3  *
4  * Copyright 2014 Melissa O'Neill <oneill@pcg-random.org>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * For additional information about the PCG random number generation scheme,
19  * including its license and other licensing options, visit
20  *
21  *     http://www.pcg-random.org
22  */
23 
24 /*
25  * This is the original demo application from the PCG library ported to the new API
26  */
27 
28 #include <stdio.h>
29 #include <stddef.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <stdbool.h>
33 #include <time.h>
34 #include <string.h>
35 
36 #include "pcg32.h"
37 
main(int argc,char ** argv)38 int main(int argc, char** argv) {
39     // Read command-line options
40     int rounds = 5;
41 
42     if (argc > 1)
43         rounds = atoi(argv[1]);
44 
45     pcg32 rng;
46 
47     // You should *always* seed the RNG.  The usual time to do it is the
48     // point in time when you create RNG (typically at the beginning of the
49     // program).
50     //
51     // pcg32::seed takes two 64-bit constants (the initial state, and the
52     // rng sequence selector; rngs with different sequence selectors will
53     // *never* have random sequences that coincide, at all)
54     rng.seed(42u, 54u);
55 
56     printf("pcg32_random_r:\n"
57            "      -  result:      32-bit unsigned int (uint32_t)\n"
58            "      -  period:      2^64   (* 2^63 streams)\n"
59            "      -  state type:  pcg32_random_t (%zu bytes)\n"
60            "      -  output func: XSH-RR\n"
61            "\n",
62            sizeof(pcg32));
63 
64     for (int round = 1; round <= rounds; ++round) {
65         printf("Round %d:\n", round);
66         /* Make some 32-bit numbers */
67         printf("  32bit:");
68         for (int i = 0; i < 6; ++i)
69             printf(" 0x%08x", rng.nextUInt());
70         printf("\n");
71 
72         /* Toss some coins */
73         printf("  Coins: ");
74         for (int i = 0; i < 65; ++i)
75             printf("%c", rng.nextUInt(2) ? 'H' : 'T');
76         printf("\n");
77 
78         /* Roll some dice */
79         printf("  Rolls:");
80         for (int i = 0; i < 33; ++i) {
81             printf(" %d", (int)rng.nextUInt(6) + 1);
82         }
83         printf("\n");
84 
85         /* Deal some cards */
86         enum { SUITS = 4, NUMBERS = 13, CARDS = 52 };
87         char cards[CARDS];
88 
89         for (int i = 0; i < CARDS; ++i)
90             cards[i] = i;
91 
92         rng.shuffle(cards, cards + CARDS);
93 
94         printf("  Cards:");
95         static const char number[] = {'A', '2', '3', '4', '5', '6', '7',
96                                       '8', '9', 'T', 'J', 'Q', 'K'};
97         static const char suit[] = {'h', 'c', 'd', 's'};
98         for (int i = 0; i < CARDS; ++i) {
99             printf(" %c%c", number[cards[i] / SUITS], suit[cards[i] % SUITS]);
100             if ((i + 1) % 22 == 0)
101                 printf("\n\t");
102         }
103         printf("\n");
104 
105         printf("\n");
106     }
107 
108     return 0;
109 }
110