Blame view

3rdparty/boost_1_81_0/libs/random/example/die.cpp 1.85 KB
73ef4ff3   Hu Chunming   提交三方库
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
  // die.cpp
  //
  // Copyright (c) 2009
  // Steven Watanabe
  //
  // Distributed under the Boost Software License, Version 1.0. (See
  // accompanying file LICENSE_1_0.txt or copy at
  // http://www.boost.org/LICENSE_1_0.txt)
  
  //[die
  /*`
      For the source of this example see
      [@boost://libs/random/example/die.cpp die.cpp].
      First we include the headers we need for __mt19937
      and __uniform_int_distribution.
  */
  #include <boost/random/mersenne_twister.hpp>
  #include <boost/random/uniform_int_distribution.hpp>
  
  /*`
    We use __mt19937 with the default seed as a source of
    randomness.  The numbers produced will be the same
    every time the program is run.  One common method to
    change this is to seed with the current time (`std::time(0)`
    defined in ctime).
  */
  boost::random::mt19937 gen;
  /*`
    [note We are using a /global/ generator object here.  This
    is important because we don't want to create a new [prng
    pseudo-random number generator] at every call]
  */
  /*`
    Now we can define a function that simulates an ordinary
    six-sided die.
  */
  int roll_die() {
      /*<< __mt19937 produces integers in the range [0, 2[sup 32]-1].
          However, we want numbers in the range [1, 6].  The distribution
          __uniform_int_distribution performs this transformation.
          [warning Contrary to common C++ usage __uniform_int_distribution
          does not take a /half-open range/.  Instead it takes a /closed range/.
          Given the parameters 1 and 6, __uniform_int_distribution
          can produce any of the values 1, 2, 3, 4, 5, or 6.]
      >>*/
      boost::random::uniform_int_distribution<> dist(1, 6);
      /*<< A distribution is a function object.  We generate a random
          number by calling `dist` with the generator.
      >>*/
      return dist(gen);
  }
  //]
  
  #include <iostream>
  
  int main() {
      for(int i = 0; i < 10; ++i) {
          std::cout << roll_die() << std::endl;
      }
  }