コード例 #1
0
int main(int argc, char **argv)
{
	std::vector<Cat> cats;
	//can't move cats around in memory, due to world essentially saving their pointers
	//given the lambdas.
	cats.reserve(5);

	std::random_device rd;
	std::mt19937 random_engine(rd());
	std::uniform_real_distribution<float> dist(0, 800.f);

	World world(800, 800);
	Mouse mouse(world, 50.f, 50.f);

	for (int i = 0; i < 5; ++i)
	{
		cats.emplace_back(world, dist(random_engine), dist(random_engine));
	}

	for (int i = 0; i < 5; ++i)
	{
		float x = dist(random_engine);
		float y = dist(random_engine);
		Rect cheese(x, y, 10.f, Color::make_from_floats(1.f, 1.f, 0.f));
		world.add_object("cheese", [](float dt){}, [cheese]() {return cheese; });
	}

	Window window("mouse and cat and cheese", 800, 800);
	
	Timer timer;
	timer.Start();
	int last_update = timer.ElapsedMilliseconds();
	int last_screen_cap = timer.ElapsedMilliseconds() - 1000;
	int step = 0;
	while (window.Open())
	{
		window.PollEvents();

		if (timer.ElapsedMilliseconds() != last_update)
		{
			long elapsed = timer.ElapsedMilliseconds();
			float delta = (elapsed - last_update) / 1000.f;
			last_update = elapsed;
			world.update(delta);
		}

		world.render(window);
	
		if (timer.ElapsedMilliseconds() >= last_screen_cap + 1000)
		{
			window.PrintScreen("screen_" + std::to_string(++step) + ".bmp");
			last_screen_cap = timer.ElapsedMilliseconds();
		}

		window.Display();
		window.Clear();
	}

	return 0;
}
コード例 #2
0
ファイル: mca.hpp プロジェクト: vlabourdette/mcaila
 void t_digit_precision ()
 {
     static int bias = 0;
     if (digits<PRECISION)
     {
         IEEE_type xeta = DR(random_engine());
         inexact(PRECISION, xeta);
     }
     bias++;
 }
コード例 #3
0
// Return a list of all the matrices we want to multiply
std::vector<Matrix> GenerateProblem(size_t length,size_t max_size = 100)
	{
	std::mt19937 random_engine(0); // This could use a proper random seed
	std::uniform_int_distribution<> dist(1,100);

	std::vector<Matrix> result;
	int previous_dimension = dist(random_engine);
	for (int i=0; i< length; i++) {
		int current_dimension = dist(random_engine);
		result.push_back({previous_dimension,current_dimension});
		previous_dimension = current_dimension;
		}
	return result;
	}
コード例 #4
0
ファイル: cstrings.utf16.cpp プロジェクト: upcaste/upcaste
        void generate() {
            up::default_random_engine random_engine(static_cast<up::uint_least32_t>(up::time(nullptr) & UINT_LEAST32_MAX));

            if (gen_type_ == data_source_generate_garbage) {
                up::uniform_int_distribution<char16_t> dist(1, 0xFFFF);
                for (size_t i = 0; i < size_; ++i) {
                    data_[i] = dist(random_engine);
                }
            }
            else if (gen_type_ == data_source_generate_ascii) {
                up::uniform_int_distribution<char16_t> dist(1, 0x7F);
                for (size_t i = 0; i < size_; ++i) {
                    data_[i] = dist(random_engine);
                }
                
                require(!up::u16snchk(data_, size_));
            }
            else {
                char32_t min_char = (gen_type_ == data_source_generate_unicode) ? 0x80 : 0x01;
                up::uniform_int_distribution<char32_t> dist(min_char, 0x010FFFF);
                for (size_t i = 0; i < size_; ) {
                    char32_t codepoint = dist(random_engine);
                    char16_t u16buffer[up::u16_cur_max];
                    int length = up::u32tou16(u16buffer, codepoint);
                    if (length <= 0) {
                        continue;
                    }

                    if ((i + length) > size_) {
                        for ( ; i < size_; ++i) {
                            data_[i] = 'a';
                        }
                    }
                    else {
                        for (int j = 0; j < length; ++i, ++j) {
                            data_[i] = u16buffer[j];
                        }
                    }
                }
                
                require(!up::u16snchk(data_, size_));
            }

            data_[size_ + 0] = 0;
            data_[size_ + 1] = 0;
        }
コード例 #5
0
ファイル: anderson_gustorn.cpp プロジェクト: leios/simuleios
double random_double(T distribution) {
    return distribution(random_engine());
}
コード例 #6
0
ファイル: main.cpp プロジェクト: farnyser/pg
int main(int argc, char **argv)
{
	// using PriorityQueue = pg::heap::Binary<int>;
	using PriorityQueue = pg::heap::Pairing<int>;
	// using PriorityQueue = StdWrapper<int>;

	test("Basic Push/Pop", []()
	{
		PriorityQueue pq;

		pq.Push(10);
		pq.Push(5);
		pq.Push(8);
		pq.Push(12);
		pq.Push(1);
		pq.Push(90);
		pq.Push(9);

		assertEquals(1, pq.Pop());
		assertEquals(5, pq.Pop());
		assertEquals(8, pq.Pop());
		assertEquals(9, pq.Pop());
		assertEquals(10, pq.Pop());
		assertEquals(12, pq.Pop());
		assertEquals(90, pq.Pop());
		assertEquals(true, pq.Empty());
	});

	test("Update value", []()
	{
		PriorityQueue pq;

		pq.Push(10);
		pq.Push(5);
		pq.Push(8);
		pq.Push(12);
		pq.Push(1);
		pq.Push(90);
		pq.Push(9);

		pq.Update(70, 5);
		pq.Update(7, 12);

		assertEquals(1, pq.Pop());
		assertEquals(7, pq.Pop());
		assertEquals(8, pq.Pop());
		assertEquals(9, pq.Pop());
		assertEquals(10, pq.Pop());
		assertEquals(70, pq.Pop());
		assertEquals(90, pq.Pop());
		assertEquals(true, pq.Empty());
	});

	test("Performance", []()
	{
		constexpr auto size = 1000000;
		std::default_random_engine random_engine(time(nullptr));
		std::uniform_int_distribution<int> distribution(1,9999999);
		auto rand = std::bind ( distribution, random_engine );

		std::vector<int> test;
		PriorityQueue pq;

		for(int i = 0 ; i < size; i++)
			pq.Push(rand());

		while(!pq.Empty())
			test.push_back(pq.Pop());

		assertEquals(true, std::is_sorted(test.begin(), test.end()));
	});

	return 0;
}
コード例 #7
0
ファイル: main.cpp プロジェクト: CCJY/coliru
int main()
{
    std::vector<int> vec(10);

	std::cout << "iota:" << '\n';
	boost::iota(vec, 1);
	for (auto e : vec) std::cout << e << " ";
	std::cout << '\n';

	std::cout << "reversed:" << '\n';
	for (auto e : vec | boost::adaptors::reversed) std::cout << e << " ";
	std::cout << '\n';

	std::cout << "sums:" << '\n';
	std::vector<int> sums(10);
	boost::partial_sum(vec, begin(sums));
	for (auto e : sums) std::cout << e << " ";
	std::cout << '\n';

	// perhaps as exercise?
	std::cout << "evens:" << '\n';
	std::vector<int> evens(10);
	boost::fill(evens, 2); // std::vector<int> evens(10, 2);
	boost::partial_sum(evens, begin(evens));
	for (auto e : evens) std::cout << e << " ";
	std::cout << '\n';

	boost::partial_sum(evens | boost::adaptors::reversed, begin(evens));
	for (auto e : evens) std::cout << e << " ";
	std::cout << '\n';

	// perhaps as exercise?
	std::cout << "factorials:" << '\n';
	std::vector<int> factorials(10);
	boost::partial_sum(vec, begin(factorials), std::multiplies<int>());
	for (auto e : factorials) std::cout << e << " ";
	std::cout << '\n';

	std::cout << "die:" << '\n';
	std::random_device random_device;
	std::default_random_engine random_engine(random_device());
	int a = 1, b = 6;
	std::uniform_int_distribution<int> uniform_distribution(a, b);
	auto die = std::bind(uniform_distribution, random_engine);
	std::cout << die() << '\n';

	std::cout << "die throws:" << '\n';
	std::vector<int> die_throws(10);
	boost::generate(die_throws, die); // readability: "generate die_throws using die [duh]"
	for (auto e : die_throws) std::cout << e << " ";
	std::cout << '\n';

	std::cout << "die throws, from #2 to #5:" << '\n';
	for (auto e : die_throws | boost::adaptors::sliced(2, 5)) std::cout << e << " ";
	std::cout << '\n';

	auto count_unique_low = boost::count_if( die_throws | boost::adaptors::uniqued, [](int result) { return result <= 3; } );
	auto count_unique_high = boost::count_if( die_throws | boost::adaptors::uniqued, [](int result) { return result > 3; } );
	std::cout << "count_unique_low = " << count_unique_low << '\n';
	std::cout << "count_unique_high = " << count_unique_high << '\n';

	/*
	std::vector<int> v;
	boost::push_front(v, boost::istream_range<int>(std::cin));
	for (auto e : v) std::cout << e << " ";
	std::cout << '\n';
	*/
	std::vector<int> v;
	boost::push_back(v, boost::irange(100, 200, 3)); // first, last, step-size
	for (auto e : v) std::cout << e << " ";
	std::cout << '\n';

}