randomgen.cpp 2.0 KB

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