Skip to content

Commit

Permalink
Merge pull request #8519 from dkorpel/dice-docs
Browse files Browse the repository at this point in the history
Fix 23270 - std.random.dice is poorly documented
  • Loading branch information
RazvanN7 authored Jul 25, 2022
2 parents 4d0e74b + 80e0fa2 commit 5748ca4
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions std/random.d
Original file line number Diff line number Diff line change
Expand Up @@ -3056,8 +3056,16 @@ if (isRandomAccessRange!Range)
}

/**
Rolls a dice with relative probabilities stored in $(D
proportions). Returns the index in `proportions` that was chosen.
Get a random index into a list of weights corresponding to each index
Similar to rolling a die with relative probabilities stored in `proportions`.
Returns the index in `proportions` that was chosen.
Note:
Usually, dice are 'fair', meaning that each side has equal probability
to come up, in which case `1 + uniform(0, 6)` can simply be used.
In future Phobos versions, this function might get renamed to something like
`weightedChoice` to avoid confusion.
Params:
rnd = (optional) random number generator to use; if not
Expand Down Expand Up @@ -3103,6 +3111,9 @@ if (isNumeric!Num)
///
@safe unittest
{
auto d6 = 1 + dice(1, 1, 1, 1, 1, 1); // fair dice roll
auto d6b = 1 + dice(2, 1, 1, 1, 1, 1); // double the chance to roll '1'

auto x = dice(0.5, 0.5); // x is 0 or 1 in equal proportions
auto y = dice(50, 50); // y is 0 or 1 in equal proportions
auto z = dice(70, 20, 10); // z is 0 70% of the time, 1 20% of the time,
Expand Down

0 comments on commit 5748ca4

Please sign in to comment.