Exemplo n.º 1
0
sanguis::server::random::split_array<
	Size
>
split(
	sanguis::random_generator &_random_generator,
	sanguis::server::random::min const _min,
	sanguis::server::random::max const _max,
	sanguis::server::random::split_array<
		Size
	> const &_sizes
)
{
	typedef
	sanguis::server::random::split_array<
		Size
	>
	result_type;

	typedef
	typename
	result_type::value_type
	value_type;

	typedef
	typename
	result_type::size_type
	size_type;

	typedef
	sanguis::server::random::distributor<
		value_type,
		size_type
	>
	distributor_type;

	distributor_type dist(
		sanguis::server::random::map_with_index(
			_sizes
		)
	);

	result_type result(
		fcppt::container::array::init_const<
			result_type
		>(
			sanguis::server::random::amount(
				0u
			)
		)
	);

	typedef
	fcppt::random::distribution::parameters::uniform_int<
		sanguis::server::random::amount
	>
	size_parameters;

	sanguis::server::random::amount const draws(
		fcppt::random::distribution::make_basic(
			size_parameters(
				size_parameters::min(
					_min.get()
				),
				size_parameters::max(
					_max.get()
				)
			)
		)(
			_random_generator
		)
	);

	fcppt::algorithm::repeat(
		draws.get(),
		[
			&_random_generator,
			&dist,
			&result
		]()
		{
			++result[
				dist.execute(
					_random_generator
				)
			];
		}
	);

	return
		result;
}
Exemplo n.º 2
0
Result
draw(
	sanguis::random_generator &_random_generator,
	Source const &_source,
	sanguis::server::random::amount const _draws,
	sanguis::server::random::create_function<
		typename
		Result::value_type,
		typename
		Source::value_type
	> const &_create_function,
	sanguis::server::random::less_function<
		typename
		Result::value_type
	> const &_less_function,
	sanguis::server::random::equal_function<
		typename
		Result::value_type
	> const &_equal_function
)
{
	return
		fcppt::optional::maybe(
			fcppt::random::wrapper::make_uniform_container(
				_source
			),
			[]{
				return
					Result();
			},
			[
				&_random_generator,
				&_less_function,
				&_create_function,
				&_equal_function,
				&_source,
				&_draws
			](
				fcppt::random::wrapper::uniform_container<
					Source const
				> _container_distribution
			)
			{
				Result result;

				result.reserve(
					std::min(
						fcppt::cast::size<
							typename
							Source::size_type
						>(
							_draws.get()
						),
						_source.size()
					)
				);

				fcppt::algorithm::repeat(
					result.capacity(),
					[
						&_create_function,
						&_random_generator,
						&_container_distribution,
						&result
					]()
					{
						result.push_back(
							_create_function.get()(
								_container_distribution(
									_random_generator
								)
							)
						);
					}
				);

				std::sort(
					result.begin(),
					result.end(),
					_less_function.get()
				);

				result.erase(
					std::unique(
						result.begin(),
						result.end(),
						_equal_function.get()
					),
					result.end()
				);

				return
					result;
			}
		);
}