// warning---this will reuse draws from transformed data // if we initialize with zero TEST(rng, initialize_with_zero) { boost::ecuyer1988 rng1 = stan::services::util::create_rng(0, 0); boost::ecuyer1988 rng2 = stan::services::util::create_rng(0, 0); EXPECT_EQ(rng1, rng2); rng2(); EXPECT_NE(rng1, rng2); }
TEST(rng, initialize_with_seed) { boost::ecuyer1988 rng1 = stan::services::util::create_rng(0, 1); boost::ecuyer1988 rng2 = stan::services::util::create_rng(0, 1); EXPECT_EQ(rng1, rng2); rng2(); // generate a random number EXPECT_NE(rng1, rng2); }
int main() { typedef qfcl::random::cpp_rand ENG; typedef qfcl::random::normal_box_muller_polar<> DIST1; typedef qfcl::random::normal_box_muller<> DIST2; typedef qfcl::random::normal_inversion<> DIST3; ENG eng; DIST1 dist1; DIST2 dist2; DIST3 dist3; qfcl::random::variate_generator< ENG, DIST1 > rng1(eng, dist1); qfcl::random::variate_generator< ENG, DIST2 > rng2(eng, dist2); qfcl::random::variate_generator< ENG, DIST3 > rng3(eng, dist3); for (int i=0; i<10; ++i) { std::cout << rng1() << " " << rng2() << " " << rng3() << std::endl; } return 0; }
int main() { SimpleRNG<int> rng(1, 10); std::vector<int> hist(11, 0); for (size_t i = 0; i < 10000; ++i) { int r = rng(); if (r < 1 || r > 10) { throw std::domain_error("Bad random"); } ++hist[r]; } for (size_t i = 0; i < hist.size(); ++i) { std::cout << i << ": " << hist[i] << "\n"; } SimpleRNG<double> rng2(0.0, 3.0); for (size_t i = 0; i < 10; ++i) { std::cout << rng2() << "\n"; } return 0; }
void copy_generate(RNG& rng) { RNG rng2{rng}; bool same=true; for (int j=0; j<5; ++j) { uint32_t a=rng(); uint32_t b=rng2(); if (a!=b) { same=false; std::cout<< a << "\t" << b << std::endl; } } if (same) { std::cout << "Same results" << std::endl; } }
int main(int argc, char* argv[]) { using RNG=afidd::rng::mt19937; RNG rng{1}; RNG rng2{1}; bool same=(rng==rng2); std::cout << "same? "<< same << std::endl; assert(rng==rng2); copy_generate(rng); { std::ofstream outfile("randgen.txt", std::fstream::out); outfile << rng; } copy_generate(rng2); { std::ifstream infile("randgen.txt", std::fstream::in); infile >> rng2; } assert(rng2==rng); assert(rng2()==rng()); std::uniform_real_distribution<double> dist(0.0, 1.0); double a=1000; double b=-1000; afidd::rng::CountedGenerator<RNG> cg{rng}; for (int i=0; i<1000000; ++i) { double val=dist(cg); if (val<a) a=val; if (val>b) b=val; } std::cout << a << std::endl; std::cout << b << std::endl; std::cout << "Used " << cg.Count() << " samples." << std::endl; //std::vector<uint32_t> vals{10000, 0}; //rng.generate(&vals[0], &vals[10000]); copy_generate(rng); return 0; }
TEST(AESCTRDRNG, SeedConsistency) { AESCTRDRBG rng1("seed1", "seed1") ; AESCTRDRBG rng2("seed1", "seed1") ; AESCTRDRBG rng3("seed2", "seed1") ; AESCTRDRBG rng4("seed1", "seed2") ; AESCTRDRBG rng5("seed2", "seed2") ; AESCTRDRBG rng6("seed2", "seed2") ; std::vector<uint8_t> bits[6] ; for(int r=0;r<5;r++) { std::vector<uint8_t> tmp ; tmp = rng1.GetBits(128) ; bits[0].insert(bits[0].end(), tmp.begin(), tmp.end()) ; tmp = rng2.GetBits(128) ; bits[1].insert(bits[1].end(), tmp.begin(), tmp.end()) ; tmp = rng3.GetBits(128) ; bits[2].insert(bits[2].end(), tmp.begin(), tmp.end()) ; tmp = rng4.GetBits(128) ; bits[3].insert(bits[3].end(), tmp.begin(), tmp.end()) ; tmp = rng5.GetBits(128) ; bits[4].insert(bits[4].end(), tmp.begin(), tmp.end()) ; tmp = rng6.GetBits(128) ; bits[5].insert(bits[5].end(), tmp.begin(), tmp.end()) ; } ASSERT_EQ(bits[0], bits[1]) ; ASSERT_EQ(bits[4], bits[5]) ; ASSERT_NE(bits[1], bits[2]) ; ASSERT_NE(bits[1], bits[3]) ; ASSERT_NE(bits[1], bits[4]) ; ASSERT_NE(bits[2], bits[3]) ; ASSERT_NE(bits[2], bits[4]) ; ASSERT_NE(bits[3], bits[4]) ; }
void mitk::ConnectomicsSyntheticNetworkGenerator::GenerateSyntheticRandomNetwork( mitk::ConnectomicsNetwork::Pointer network, int numberOfPoints, double threshold ) { // as the surface is proportional to the square of the radius the density stays the same double radius = 5 * std::sqrt( (float) numberOfPoints ); //the random number generators unsigned int randomOne = (unsigned int) rand(); unsigned int randomTwo = (unsigned int) rand(); vnl_random rng( (unsigned int) rand() ); vnl_random rng2( (unsigned int) rand() ); // map for storing the conversion from indices to vertex descriptor std::map< int, mitk::ConnectomicsNetwork::VertexDescriptorType > idToVertexMap; //add vertices on sphere surface for( int loopID( 0 ); loopID < numberOfPoints; loopID++ ) { std::vector< float > position; std::string label; std::stringstream labelStream; labelStream << loopID; label = labelStream.str(); //generate random, uniformly distributed points on a sphere surface const double uVariable = rng.drand64( 0.0 , 1.0); const double vVariable = rng.drand64( 0.0 , 1.0); const double phi = 2 * vnl_math::pi * uVariable; const double theta = std::acos( 2 * vVariable - 1 ); double xpos = radius * std::cos( phi ) * std::sin( theta ); double ypos = radius * std::sin( phi ) * std::sin( theta ); double zpos = radius * std::cos( theta ); position.push_back( xpos ); position.push_back( ypos ); position.push_back( zpos ); mitk::ConnectomicsNetwork::VertexDescriptorType newVertex = network->AddVertex( loopID ); network->SetLabel( newVertex, label ); network->SetCoordinates( newVertex, position ); if ( idToVertexMap.count( loopID ) > 0 ) { MITK_ERROR << "Aborting network creation, duplicate vertex ID generated."; m_LastGenerationWasSuccess = false; return; } idToVertexMap.insert( std::pair< int, mitk::ConnectomicsNetwork::VertexDescriptorType >( loopID, newVertex) ); } int edgeID(0); // uniform weight of one int edgeWeight(1); mitk::ConnectomicsNetwork::VertexDescriptorType source; mitk::ConnectomicsNetwork::VertexDescriptorType target; for( int loopID( 0 ); loopID < numberOfPoints; loopID++ ) { // to avoid creating an edge twice (this being an undirected graph) we only // potentially generate edges with all nodes with a bigger ID for( int innerLoopID( loopID ); innerLoopID < numberOfPoints; innerLoopID++ ) { if( rng.drand64( 0.0 , 1.0) > threshold) { // do nothing } else { source = idToVertexMap.find( loopID )->second; target = idToVertexMap.find( innerLoopID )->second; network->AddEdge( source, target, loopID, innerLoopID, edgeWeight); edgeID++; } } // end for( int innerLoopID( loopID ); innerLoopID < numberOfPoints; innerLoopID++ ) } // end for( int loopID( 0 ); loopID < numberOfPoints; loopID++ ) m_LastGenerationWasSuccess = true; }
void mitk::ConnectomicsSyntheticNetworkGenerator::GenerateSyntheticCenterToSurfaceNetwork( mitk::ConnectomicsNetwork::Pointer network, int numberOfPoints, double radius ) { //the random number generators unsigned int randomOne = (unsigned int) rand(); unsigned int randomTwo = (unsigned int) rand(); vnl_random rng( (unsigned int) rand() ); vnl_random rng2( (unsigned int) rand() ); mitk::ConnectomicsNetwork::VertexDescriptorType centerVertex; int vertexID(0); { //add center vertex std::vector< float > position; std::string label; std::stringstream labelStream; labelStream << vertexID; label = labelStream.str(); position.push_back( 0 ); position.push_back( 0 ); position.push_back( 0 ); centerVertex = network->AddVertex( vertexID ); network->SetLabel( centerVertex, label ); network->SetCoordinates( centerVertex, position ); }//end add center vertex // uniform weight of one int edgeWeight(1); mitk::ConnectomicsNetwork::VertexDescriptorType source; mitk::ConnectomicsNetwork::VertexDescriptorType target; //add vertices on sphere surface for( int loopID( 1 ); loopID < numberOfPoints; loopID++ ) { std::vector< float > position; std::string label; std::stringstream labelStream; labelStream << loopID; label = labelStream.str(); //generate random, uniformly distributed points on a sphere surface const double uVariable = rng.drand64( 0.0 , 1.0); const double vVariable = rng.drand64( 0.0 , 1.0); const double phi = 2 * vnl_math::pi * uVariable; const double theta = std::acos( 2 * vVariable - 1 ); double xpos = radius * std::cos( phi ) * std::sin( theta ); double ypos = radius * std::sin( phi ) * std::sin( theta ); double zpos = radius * std::cos( theta ); position.push_back( xpos ); position.push_back( ypos ); position.push_back( zpos ); mitk::ConnectomicsNetwork::VertexDescriptorType newVertex = network->AddVertex( loopID ); network->SetLabel( newVertex, label ); network->SetCoordinates( newVertex, position ); network->AddEdge( newVertex, centerVertex, loopID, 0, edgeWeight); } m_LastGenerationWasSuccess = true; }
int main(int argc, char **argv) { // RNGs RNG rng(0); RNG rng2(1); /* ********************************************************************** * Print program information ********************************************************************** */ std::cout << "Psychopath v" << VERSION_MAJOR << "." << VERSION_MINOR << "." << VERSION_PATCH; #ifdef DEBUG std::cout << " (DEBUG build)"; #endif std::cout << std::endl; #ifdef DEBUG std::cout << std::endl << "Struct sizes:" << std::endl; std::cout << "\tvoid*: " << sizeof(void*) << std::endl; std::cout << "\tVec3: " << sizeof(Vec3) << std::endl; std::cout << "\tBBox: " << sizeof(BBox) << std::endl; std::cout << "\tRay: " << sizeof(Ray) << std::endl; std::cout << "\tIntersection: " << sizeof(Intersection) << std::endl; std::cout << "\tPotentialInter: " << sizeof(PotentialInter) << std::endl; std::cout << "\tBVH::Node: " << sizeof(BVH::Node) << std::endl; #endif /* ********************************************************************** * Command-line options. ********************************************************************** */ int spp = SPP; int spp_max = SPP; float variance_max = -1.0f; int threads = std::thread::hardware_concurrency(); std::string output_path = "default.png"; std::string input_path = ""; Resolution resolution(XRES, YRES); // Define them BPO::options_description desc("Allowed options"); desc.add_options() ("help,h", "Print this help message") ("scenefile,i", BPO::value<std::string>(), "Input scene file") ("spp,s", BPO::value<int>(), "Number of samples to take per pixel") ("sppmax,m", BPO::value<int>(), "Max number of samples to take per pixel") ("variance,v", BPO::value<float>(), "Max image variance") ("threads,t", BPO::value<int>(), "Number of threads to render with") ("output,o", BPO::value<std::string>(), "The PNG file to render to") ("nooutput,n", "Don't save render (for timing tests)") ("resolution,r", BPO::value<Resolution>()->multitoken(), "The resolution to render at, e.g. 1280 720") ; // Collect them BPO::variables_map vm; BPO::store(BPO::parse_command_line(argc, argv, desc), vm); BPO::notify(vm); // Help message if (vm.count("help")) { std::cout << desc << "\n"; return 1; } // Suppress image writing Config::no_output = bool(vm.count("nooutput")); // Samples per pixel if (vm.count("spp")) { spp = vm["spp"].as<int>(); if (spp < 1) spp = 1; std::cout << "Samples per pixel: " << spp << "\n"; } if (vm.count("sppmax")) { spp_max = vm["sppmax"].as<int>(); if (spp_max < spp) spp_max = spp; std::cout << "Max samples per pixel: " << spp_max << "\n"; } if (vm.count("variance")) { variance_max = vm["variance"].as<float>(); std::cout << "Max image variance: " << variance_max << "\n"; } // Thread count if (vm.count("threads")) { threads = vm["threads"].as<int>(); if (threads < 1) threads = 1; std::cout << "Threads: " << threads << "\n"; } // Input file if (vm.count("scenefile")) { input_path = vm["scenefile"].as<std::string>(); std::cout << "Input scene: " << input_path << "\n"; } // Output file if (vm.count("output")) { output_path = vm["output"].as<std::string>(); std::cout << "Output path: " << output_path << "\n"; } // Resolution if (vm.count("resolution")) { resolution = vm["resolution"].as<Resolution>(); std::cout << "Resolution: " << resolution.x << " " << resolution.y << "\n"; } std::cout << std::endl; /* *********************************************** * Parse scene file, rendering frames as we go. *********************************************** */ Parser parser(input_path); Timer<> total_timer; while (true) { Timer<> parse_timer; std::unique_ptr<Renderer> r {parser.parse_next_frame()}; if (r == nullptr) break; std::cout << "Parse time (seconds): " << parse_timer.time() << std::endl; Timer<> preprocessing_timer; r->scene->finalize(); std::cout << "Preprocessing time (seconds): " << preprocessing_timer.time() << std::endl; // Resolution and sampling overrides if (vm.count("resolution")) r->set_resolution(resolution.x, resolution.y); if (vm.count("spp")) r->set_spp(spp); if (vm.count("sppmax")) r->set_spp_max(spp_max); if (vm.count("variance")) r->set_variance_max(variance_max); /* ********************************************************************** * Generate image ********************************************************************** */ r->render(threads); std::cout << std::endl << std::endl; } std::cout << "Total time (seconds): " << std::fixed << std::setprecision(3) << total_timer.time() << std::endl << std::endl; return 0; }
inline bool random_rng_d(std::size_t N, std::size_t M) { using result_type = typename RNGType::result_type; mckl::UniformIntDistribution<std::size_t> rsize(0, N); mckl::UniformBitsDistribution<std::uint16_t> ubits16; mckl::UniformBitsDistribution<std::uint32_t> ubits32; mckl::UniformBitsDistribution<std::uint64_t> ubits64; RNGType rng; bool pass = true; RNGType rng1; RNGType rng2; mckl::Vector<result_type> r1; mckl::Vector<result_type> r2; mckl::Vector<std::uint16_t> u16_1; mckl::Vector<std::uint16_t> u16_2; mckl::Vector<std::uint32_t> u32_1; mckl::Vector<std::uint32_t> u32_2; mckl::Vector<std::uint64_t> u64_1; mckl::Vector<std::uint64_t> u64_2; r1.reserve(N); r2.reserve(N); u16_1.reserve(N); u16_2.reserve(N); u32_1.reserve(N); u32_2.reserve(N); u64_1.reserve(N); u64_2.reserve(N); for (std::size_t i = 0; i != M; ++i) { std::size_t K = rsize(rng); r1.resize(K); r2.resize(K); u16_1.resize(K); u16_2.resize(K); u32_1.resize(K); u32_2.resize(K); u64_1.resize(K); u64_2.resize(K); for (std::size_t j = 0; j != K; ++j) { r1[j] = rng1(); } mckl::rand(rng2, K, r2.data()); pass = pass && (r1 == r2 || rng != rng); for (std::size_t j = 0; j != K; ++j) { u16_1[j] = mckl::rand(rng1, ubits16); } mckl::rand(rng2, ubits16, K, u16_2.data()); pass = pass && (u16_1 == u16_2 || rng != rng); for (std::size_t j = 0; j != K; ++j) { u32_1[j] = mckl::rand(rng1, ubits32); } mckl::rand(rng2, ubits32, K, u32_2.data()); pass = pass && (u32_1 == u32_2 || rng != rng); for (std::size_t j = 0; j != K; ++j) { u64_1[j] = mckl::rand(rng1, ubits64); } mckl::rand(rng2, ubits64, K, u64_2.data()); pass = pass && (u64_1 == u64_2 || rng != rng); rng1.discard(static_cast<unsigned>(K)); typename RNGType::result_type next = rng1(); for (std::size_t j = 0; j != K; ++j) { rng2(); } bool find = false; for (std::size_t j = 0; j != 2; ++j) { find = find || rng2() == next; } pass = pass && (find || rng != rng); std::stringstream ss; ss << rng; mckl::rand(rng, K, r1.data()); ss >> rng; mckl::rand(rng, K, r2.data()); pass = pass && (r1 == r2 || rng != rng); RNGType tmp(rng); mckl::rand(rng, K, r1.data()); mckl::rand(tmp, K, r2.data()); pass = pass && (r1 == r2 || rng != rng); tmp = rng; mckl::rand(rng, K, r1.data()); mckl::rand(tmp, K, r2.data()); pass = pass && (r1 == r2 || rng != rng); } return pass; }