]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/sort/example/randomgen.cpp
update sources to v12.2.4
[ceph.git] / ceph / src / boost / libs / sort / example / randomgen.cpp
CommitLineData
3a9019d9
FG
1// flexible random number generator providing multiple distributions.\r
2//\r
3// Copyright Steven Ross 2009-2014.\r
4//\r
5// Distributed under the Boost Software License, Version 1.0.\r
6// (See accompanying file LICENSE_1_0.txt or copy at\r
7// http://www.boost.org/LICENSE_1_0.txt)\r
8\r
9// See http://www.boost.org/libs/sort for library home page.\r
10\r
11#include <boost/random/mersenne_twister.hpp>\r
12#include <boost/random/uniform_int_distribution.hpp>\r
13#include <stdio.h>\r
14#include "stdlib.h"\r
15#include <fstream>\r
16#include <iostream>\r
17using namespace boost;\r
18\r
19int main(int argc, const char ** argv) {\r
20 random::mt19937 generator;\r
21 random::uniform_int_distribution<unsigned> distribution;\r
22 //defaults\r
23 unsigned high_shift = 16;\r
24 unsigned low_shift = 16;\r
25 unsigned count = 1000000;\r
26 //Reading in user arguments\r
27 if (argc > 1)\r
28 high_shift = atoi(argv[1]);\r
29 if (argc > 2)\r
30 low_shift = atoi(argv[2]);\r
31 if (argc > 3)\r
32 count = atoi(argv[3]);\r
33 if (high_shift > 16)\r
34 high_shift = 16;\r
35 if (low_shift > 16)\r
36 low_shift = 16;\r
37 std::ofstream ofile;\r
38 ofile.open("input.txt", std::ios_base::out | std::ios_base::binary |\r
39 std::ios_base::trunc);\r
40 if (ofile.bad()) {\r
41 printf("could not open input.txt for writing!\n");\r
42 return 1;\r
43 }\r
44 //buffering file output for speed\r
45 unsigned uDivideFactor = 1000;\r
46 //Skipping buffering for small files\r
47 if (count < uDivideFactor * 100)\r
48 uDivideFactor = count;\r
49 unsigned * pNumbers = static_cast<unsigned *>(malloc(uDivideFactor * \r
50 sizeof(unsigned)));\r
51 //Generating semirandom numbers\r
52 unsigned mask = 0;\r
53 unsigned one = 1;\r
54 for (unsigned u = 0; u < low_shift; ++u) {\r
55 mask += one << u;\r
56 }\r
57 for (unsigned u = 0; u < high_shift; ++u) {\r
58 mask += one << (16 + u);\r
59 }\r
60 for (unsigned u = 0; u < count/uDivideFactor; ++u) {\r
61 unsigned i = 0;\r
62 for (; i< uDivideFactor; ++i) {\r
63 pNumbers[i] = distribution(generator) & mask;\r
64 }\r
65 ofile.write(reinterpret_cast<char *>(pNumbers), uDivideFactor * 4 );\r
66 }\r
67 ofile.close();\r
68 return 0;\r
69}\r