Skip to content

Commit 95b21d9

Browse files
committed
Document the shuffle function
1 parent 6e4d1e8 commit 95b21d9

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

lib/stdlib/src/rand.erl

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,13 +1317,42 @@ normal_s(Mean, Variance, State0) when 0 =< Variance ->
13171317
{Mean + (math:sqrt(Variance) * X), State}.
13181318

13191319

1320-
-spec shuffle(list()) -> list().
1320+
-doc """
1321+
Shuffle a list.
1322+
1323+
Like `shuffle_s/2` but operates on the state stored in
1324+
the process dictionary. Returns the shuffled list.
1325+
""".
1326+
-doc(#{group => <<"Plug-in framework API">>,since => <<"OTP 29.0">>}).
1327+
-spec shuffle(List :: list()) -> ShuffledList :: list().
13211328
shuffle(List) ->
13221329
{ShuffledList, State} = shuffle_s(List, seed_get()),
13231330
_ = seed_put(State),
13241331
ShuffledList.
13251332

1326-
-spec shuffle_s(list(), state()) -> {list(), state()}.
1333+
-doc """
1334+
Shuffle a list.
1335+
1336+
From the specified `State` shuffles the elements in argument `List` so that,
1337+
given that the [PRNG algorithm](#algorithms) in `State` is perfect,
1338+
every possible permutation of the elements in the returned `ShuffledList`
1339+
has the same probability.
1340+
1341+
In other words, the quality of the shuffling depends only on
1342+
the quality of the backend [random number generator](#algorithms)
1343+
and [seed](`seed_s/1`). If a cryptographically unpredictable
1344+
shuffling is needed, use for example `crypto:rand_seed_alg_s/1`
1345+
to initialize the random number generator.
1346+
1347+
Returns the shuffled list [`ShuffledList`](`t:list/0`)
1348+
and the [`NewState`](`t:state/0`).
1349+
""".
1350+
-doc(#{group => <<"Plug-in framework API">>,since => <<"OTP 29.0">>}).
1351+
-spec shuffle_s(List, State) ->
1352+
{ShuffledList :: list(), NewState :: state()}
1353+
when
1354+
List :: list(),
1355+
State :: state().
13271356
shuffle_s(List, {#{bits:=_, next:=Next} = AlgHandler, R0})
13281357
when is_list(List) ->
13291358
WeakLowBits = maps:get(weak_low_bits, AlgHandler, 0),

0 commit comments

Comments
 (0)