TestData::TestData() { numelem = 1000; weight.resize(numelem, 0); value.resize(numelem, 0); sum = 0; unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); mt19937_64 generator(seed); for (int i = 0; i < numelem; i++) { weight[i] = generator()%numelem + 1; sum += weight[i]; value[i] = weight[i] + 50; } capacity = sum / 2; //picked this interval without any underlying criteria // unifdist(0,1000); default_random_engine random_engine; uniform_int_distribution<int> unifdist(1,numelem); for (int i = 0; i < numelem; i++) { weight[i] = unifdist(random_engine); sum += weight[i]; value[i] = weight[i] + 50; } capacity = sum / 2; }
int main() { // std::default_random_engine generator; std::mt19937 generator; std::uniform_int_distribution<int> distribution(1,6); std::normal_distribution<double> normdist(0.0, 1.0); std::uniform_real_distribution<double> unifdist(0.0, 1.0); // Bind the generator to the first argument of distribution so that // we can call with rng() instead of dist(gen). auto rng = std::bind( unifdist, generator ); for( int i=0; i<25; ++i ) { std::cout << rng() << " "; } std::cout << std::endl; // Now create a random matrix std::valarray<double> v(100); for (int i=0; i < v.size(); i++ ) { v[i] = rng(); } return 0; }
TestData::TestData(int numel, bool verbose) { numelem = numel; weight.resize(numelem, 0); value.resize(numelem, 0); sum = 0; default_random_engine random_engine; uniform_int_distribution<int> unifdist(1,numelem); for (int i = 0; i < numelem; i++) { weight[i] = rand()%numelem + 1;//unifdist(random_engine); sum += weight[i]; value[i] = weight[i]+rand()%numelem + 1; //+ 50; if(verbose) cout << i << "weight: " << weight[i] << "\t" << "value: " << value[i] << endl; } capacity = sum / 2; if(verbose) cout << "Capacity: " << capacity << endl; }
result sim( std::function<int(double,double)> const& f, len_t N) { // Bind the generator to the first argument of distribution so that // we can call with rng() instead of dist(gen). // auto rng = std::bind( unifdist, generator ); len_t sum = 0; for( len_t i=0; i<N; ++i) { double x = unifdist(generator); double y = unifdist(generator); sum += f(x,y); } result res; res.Mean = (1.0*sum)/N; res.StDev = sqrt( res.Mean*(1.0 - res.Mean) ); res.Total = sum; res.PointCount = N; return res; };