Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Simox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Florian Leander Singer
Simox
Commits
9080d5d4
Commit
9080d5d4
authored
2 years ago
by
Rainer Kartmann
Browse files
Options
Downloads
Patches
Plain Diff
Add simox::random::choice()
parent
575afe83
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
SimoxUtility/CMakeLists.txt
+1
-0
1 addition, 0 deletions
SimoxUtility/CMakeLists.txt
SimoxUtility/random.h
+5
-0
5 additions, 0 deletions
SimoxUtility/random.h
SimoxUtility/random/choice.h
+39
-0
39 additions, 0 deletions
SimoxUtility/random/choice.h
with
45 additions
and
0 deletions
SimoxUtility/CMakeLists.txt
+
1
−
0
View file @
9080d5d4
...
...
@@ -220,6 +220,7 @@ SET(INCLUDES
meta/enum/EnumNames.hpp
meta/key_type.h
random/choice.h
simox/SimoxPath.h
...
...
This diff is collapsed.
Click to expand it.
SimoxUtility/random.h
0 → 100644
+
5
−
0
View file @
9080d5d4
#pragma once
// This file is generated!
#include
"random/choice.h"
This diff is collapsed.
Click to expand it.
SimoxUtility/random/choice.h
0 → 100644
+
39
−
0
View file @
9080d5d4
#pragma once
#include
<random>
#include
<stdexcept>
#include
<vector>
#include
<SimoxUtility/error/SimoxError.h>
namespace
simox
::
random
{
/**
* Return a random element from the non-empty vector items.
*
* @throw `std::out_of_range` if items is empty.
*/
template
<
class
T
,
class
RandomEngineT
>
const
T
&
choice
(
const
std
::
vector
<
T
>&
items
,
RandomEngineT
&
gen
)
{
if
(
items
.
empty
())
{
throw
error
::
SimoxError
(
"Cannot choose an item from an empty vector."
);
}
std
::
uniform_int_distribution
<
size_t
>
distrib
(
0
,
items
.
size
()
-
1
);
std
::
size_t
index
=
distrib
(
gen
);
return
items
.
at
(
index
);
}
template
<
class
T
>
const
T
&
choice
(
const
std
::
vector
<
T
>&
items
)
{
std
::
default_random_engine
gen
{
std
::
random_device
{}()};
return
choice
(
items
,
gen
);
}
}
// namespace simox::random
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment