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$ Commented Feb 8, 2018 at 17:04

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.