Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
coding-streams
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
coding_tug
coding-streams
Commits
fbcd9ae7
Commit
fbcd9ae7
authored
4 years ago
by
Guttmann, Michael
Browse files
Options
Downloads
Patches
Plain Diff
singleton + random
parent
2614aabe
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
2021-oop1/ku/stream-05/Dice.cpp
+31
-0
31 additions, 0 deletions
2021-oop1/ku/stream-05/Dice.cpp
2021-oop1/ku/stream-05/Dice.hpp
+23
-0
23 additions, 0 deletions
2021-oop1/ku/stream-05/Dice.hpp
2021-oop1/ku/stream-05/main.cpp
+24
-0
24 additions, 0 deletions
2021-oop1/ku/stream-05/main.cpp
with
78 additions
and
0 deletions
2021-oop1/ku/stream-05/Dice.cpp
0 → 100644
+
31
−
0
View file @
fbcd9ae7
#include
"Dice.hpp"
Dice
::
Dice
()
:
mt_
(),
distribution_
(
1
,
6
)
{
std
::
random_device
rd
;
mt_
.
seed
(
rd
());
}
Dice
&
Dice
::
getInstance
()
{
static
Dice
dice
;
return
dice
;
}
unsigned
int
Dice
::
roll
()
{
// return (mt() % 6) + 1; // ist problematisch
// Beispiel anhand eines 4-bit Integers, der Zahlen im Bereich [0, 15] darstellen kann.
// Wahrscheinlichkeiten der einzelnen Zahlen des Würfels:
// 1: 0, 6, 12 -> WSK = 3/16
// 2: 1, 7, 13
// 3: 2, 8, 14
// 4: 3, 9, 15
// 5: 4, 10 -> WSK = 2/16
// 6: 5, 11
// Damit ist das Ergebnis nicht "fair" bzw. gleichverteilt.
// Daher besser wie hier drunter die Gleichverteilung verwenden. :)
return
distribution_
(
mt_
);
}
This diff is collapsed.
Click to expand it.
2021-oop1/ku/stream-05/Dice.hpp
0 → 100644
+
23
−
0
View file @
fbcd9ae7
#ifndef DICE_HPP
#define DICE_HPP
#include
<random>
class
Dice
{
private:
std
::
mt19937
mt_
;
std
::
uniform_int_distribution
<
unsigned
int
>
distribution_
;
Dice
();
Dice
(
const
Dice
&
copy
)
=
delete
;
~
Dice
()
=
default
;
public:
static
Dice
&
getInstance
();
unsigned
int
roll
();
};
#endif // DICE_HPP
This diff is collapsed.
Click to expand it.
2021-oop1/ku/stream-05/main.cpp
0 → 100644
+
24
−
0
View file @
fbcd9ae7
#include
<iostream>
#include
<string>
#include
"Dice.hpp"
int
main
(
int
argc
,
char
*
argv
[])
{
// Diese Methode kann an jeder beliebigen Stelle im Programm aufgerufen
// werden. Damit kann also immer auf den Würfel zugegriffen werden.
Dice
&
dice
=
Dice
::
getInstance
();
for
(
size_t
i
=
0
;
i
<
20
;
i
++
)
{
unsigned
int
result
=
dice
.
roll
();
std
::
cout
<<
"Roll number: "
;
std
::
cout
.
width
(
2
);
std
::
cout
<<
i
+
1
;
std
::
cout
.
width
(
0
);
std
::
cout
<<
" - Result: "
<<
result
<<
std
::
endl
;
}
return
0
;
}
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