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$

1 Answer 1

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
  • 1
    $\begingroup$ applies to RandomSample as well $\endgroup$
    – Jason B.
    Feb 8, 2018 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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