Bug introduced in 8.0.4 and persisting through 13.0.1 or later (case: 4948255). It has been fixed in 13.2.0 or earlier.
I use Mathematica 12.3.1 for Microsoft Windows (64 bit). While playing with some code inspired by this recent question, I stumbled upon a call to RandomChoice
that seems to reliably crash my kernel. Playing with different methods for the random number generator, I noticed that the "Congruential"
method does not handle weighted choices well.
First, this code executes repeatedly and with no problem:
SeedRandom[20220622, Method -> "Congruential"];
RandomChoice[Range[10, 13]]
(* Out: 12 *)
The following similar call, which should in principle be equivalent but uses explicit constant weights, reliably crashed my kernel every time I ran it:
(* BEWARE - POTENTIAL KERNEL CRASH *)
SeedRandom[20220622, Method -> "Congruential"];
RandomChoice[{1, 1, 1, 1} -> Range[10, 13]]
Other values for the weights (e.g. {1, 2, 2, 1}
or {1/4, 1/4, 1/4, 1/4}
, or corresponding lists of machine-precision numbers) all have the same behavior, i.e. they lead to a crash.
Other generators are not affected:
ClearAll[tester]
tester[method_String] := (
SeedRandom[20220622, Method -> method];
{method, RandomChoice[{1, 1, 1, 1} -> Range[10, 13]]}
)
TableForm[
tester /@ {
"ParallelGenerator", "ParallelMersenneTwister",
"MersenneTwister", "MKL",
"Rule30CA", "Rule50025CA",
"ExtendedCA", "Legacy", "OpenSSL"}
]
Questions:
- Is this a bug, or am I mishandling the random number framework?
- Is there a first-principle reason why the
"Congruential"
method cannot handle weights? - Is this behavior version-dependent? OS-dependent?
13.2.0
on Windows 11. $\endgroup$