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