1
$\begingroup$

I'm pretty sure this must have been asked before, but I can not find the duplicate.

Given a list of values with their probabilities

probabilities = {{1500, 0.037000000000000005`}, {2500, 0.027000000000000003`}, {3500, 
   0.039`}, {4500, 0.07400000000000001`}, {5500, 0.073`}, {6500, 
   0.08199999999999999`}, {7500, 0.092`}, {8500, 0.096`}, {9500, 
   0.095`}, {10500, 0.09`}, {11500, 0.078`}, {12500, 0.065`}, {13500, 
   0.051`}, {14500, 0.037000000000000005`}, {15500, 
   0.026000000000000002`}, {16500, 0.017`}, {17500, 0.01`}, {18500, 
   0.006`}, {19500, 0.003`}, {20500, 0.002`}};

How can I draw a random value from this list with the given probability? I'd wager a guess that you could somehow model a PDF based on this and use it with functions like RandomVariate[].

$\endgroup$
2
  • 3
    $\begingroup$ Look at RandomChoice. $\endgroup$
    – Chris K
    Commented Sep 17, 2023 at 19:01
  • $\begingroup$ @ChrisK That was what I was looking for thanks! If you'd like the points, post it as an answer (no code example needed) $\endgroup$ Commented Sep 17, 2023 at 19:05

1 Answer 1

4
$\begingroup$
SeedRandom[1];

RandomChoice[WeightedData @@ Transpose[probabilities], 5]
{12500, 4500, 12500, 5500, 5500}

Alternatively,

SeedRandom[1];

RandomChoice[#2 -> # & @@ Transpose[probabilities], 5]
{12500, 4500, 12500, 5500, 5500}
$\endgroup$

Your Answer

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

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