File: randomgen.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (69 lines) | stat: -rw-r--r-- 2,067 bytes parent folder | download | duplicates (13)
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
61
62
63
64
65
66
67
68
69
// flexible random number generator providing multiple distributions.
//
//  Copyright Steven Ross 2009-2014.
//
// 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)

//  See http://www.boost.org/libs/sort for library home page.

#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <stdio.h>
#include "stdlib.h"
#include <fstream>
#include <iostream>
using namespace boost;

int main(int argc, const char ** argv) {
  random::mt19937 generator;
  random::uniform_int_distribution<unsigned> distribution;
  //defaults
  unsigned high_shift = 16;
  unsigned low_shift = 16;
  unsigned count = 1000000;
  //Reading in user arguments
  if (argc > 1)
    high_shift = atoi(argv[1]);
  if (argc > 2)
    low_shift = atoi(argv[2]);
  if (argc > 3)
    count = atoi(argv[3]);
  if (high_shift > 16)
    high_shift = 16;
  if (low_shift > 16)
    low_shift = 16;
  std::ofstream ofile;
  ofile.open("input.txt", std::ios_base::out | std::ios_base::binary |
             std::ios_base::trunc);
  if (ofile.bad()) {
    printf("could not open input.txt for writing!\n");
    return 1;
  }
  //buffering file output for speed
  unsigned uDivideFactor = 1000;
  //Skipping buffering for small files
  if (count < uDivideFactor * 100)
    uDivideFactor = count;
  unsigned * pNumbers = static_cast<unsigned *>(malloc(uDivideFactor * 
                                                       sizeof(unsigned)));
  //Generating semirandom numbers
  unsigned mask = 0;
  unsigned one = 1;
  for (unsigned u = 0; u < low_shift; ++u) {
    mask += one << u;
  }
  for (unsigned u = 0; u < high_shift; ++u) {
    mask += one << (16 + u);
  }
  for (unsigned u = 0; u < count/uDivideFactor; ++u) {
    unsigned i = 0;
    for (; i< uDivideFactor; ++i) {
      pNumbers[i] = distribution(generator) & mask;
    }
    ofile.write(reinterpret_cast<char *>(pNumbers), uDivideFactor * 4 );
  }
  ofile.close();
  return 0;
}