16
$\begingroup$

Why isn't the output of RandomChoice a packed array? For instance:

RandomChoice[{.5, .2, .3} -> {1, 2, 3}, 10]
Developer`PackedArrayQ @ %

{1, 2, 3, 1, 3, 1, 1, 2, 3, 3}

False

$\endgroup$
17
$\begingroup$

In order for RandomChoice to produce a packed array, the RHS of the rule needs to be packed:

pQ = Developer`PackedArrayQ;
pack = Developer`ToPackedArray;

pQ @ RandomChoice[pack[{.5, .2, .3}] -> pack[{1, 2, 3}], 10]
pQ @ RandomChoice[pack[{.5, .2, .3}] -> {1, 2, 3}, 10]
pQ @ RandomChoice[{.5, .2, .3} -> pack[{1, 2, 3}], 10]

True

False

True

$\endgroup$
  • 1
    $\begingroup$ applies to RandomSample as well $\endgroup$ – Jason B. Feb 8 '18 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.