| 1 | // a random number generator supporting different distributions. |
| 2 | // |
| 3 | // Copyright Steven Ross 2009. |
| 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 <stdio.h> |
| 12 | #include "stdlib.h" |
| 13 | #include <fstream> |
| 14 | #include <iostream> |
| 15 | #include <vector> |
| 16 | #include <string> |
| 17 | #include <boost/random.hpp> |
| 18 | |
| 19 | using std::string; |
| 20 | using namespace boost; |
| 21 | |
| 22 | int main(int argc, const char ** argv) { |
| 23 | //Always seed with the same value, to get the same results |
| 24 | srand(seed: 1); |
| 25 | //defaults |
| 26 | int mod_shift = 32; |
| 27 | unsigned count = 1000000; |
| 28 | //Reading in user arguments |
| 29 | if (argc > 2) |
| 30 | count = atoi(nptr: argv[2]); |
| 31 | if (argc > 1) |
| 32 | mod_shift = atoi(nptr: argv[1]) - 1; |
| 33 | std::ofstream ofile; |
| 34 | ofile.open(s: "input.txt" , mode: std::ios_base::out | std::ios_base::binary | |
| 35 | std::ios_base::trunc); |
| 36 | if (ofile.bad()) { |
| 37 | printf(format: "could not open input.txt for writing!\n" ); |
| 38 | return 1; |
| 39 | } |
| 40 | int min_int = (std::numeric_limits<int>::min)(); |
| 41 | int max_int = (std::numeric_limits<int>::max)(); |
| 42 | if (mod_shift < 31 && mod_shift >= 0) { |
| 43 | max_int %= 1 << mod_shift; |
| 44 | if (-max_int > min_int) |
| 45 | min_int = -max_int; |
| 46 | } |
| 47 | std::vector<int> result; |
| 48 | result.resize(new_size: count); |
| 49 | mt19937 rng; |
| 50 | if (argc > 3 && (string(argv[3]) == "-normal" )) { |
| 51 | boost::normal_distribution<> everything(0, max_int/4); |
| 52 | variate_generator<mt19937&,normal_distribution<> > gen(rng, everything); |
| 53 | generate(first: result.begin(), last: result.end(), gen: gen); |
| 54 | } |
| 55 | else if (argc > 3 && (string(argv[3]) == "-lognormal" )) { |
| 56 | lognormal_distribution<> everything(max_int/2, max_int/4); |
| 57 | variate_generator<mt19937&,lognormal_distribution<> > gen(rng, everything); |
| 58 | generate(first: result.begin(), last: result.end(), gen: gen); |
| 59 | } |
| 60 | else { |
| 61 | uniform_int<> everything(min_int, max_int); |
| 62 | variate_generator<mt19937&,uniform_int<> > gen(rng, everything); |
| 63 | generate(first: result.begin(), last: result.end(), gen: gen); |
| 64 | } |
| 65 | ofile.write(s: reinterpret_cast<char *>(&(result[0])), n: result.size() * |
| 66 | sizeof(int)); |
| 67 | ofile.close(); |
| 68 | return 0; |
| 69 | } |
| 70 | |