# Multinomial Distribution Sampler not working

I am trying to sample from a multinomial distribution many times but with a different multinomial distribution each time. The straightforward

RandomVariate[MultinomialDistribution[1,{p1, p2, ..., pN}]]


is too slow for my application. I wrote my own sampler where I use Accumulate on the distribution and then use RandomReal[] to choose one possible outcome.

S = 300;
dis = Accumulate@Table[1/S, {k, 1, S}];
GetRand[dist_] := FirstPosition[dist, SelectFirst[dist, # >= RandomReal[] &]][[1]];
test = Table[GetRand[dis], {k, 1, 100000}];
Histogram[test, S, "PDF"]


Because dis is uniform, my expectation is that the histogram should be mostly uniform. Instead I get a histogram which is clearly not uniform. I've found that I get the correct result if I separately generate a table of all the random reals and then use them to sample from the multinomial distribution.

Why does this code not give a uniform histogram? Also, why does it appear to give a distribution from the Gaussian Orthogonal Ensemble?

• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. Jun 23, 2021 at 2:21

You may want to look at the solution that I posted sometime ago. You will need a C compiler on your machine. Also note that the function does not check to ensure that the inputs for consistency with the requirements of a distribution.

Improving Speed of Multinomial Draws

If you're really wanting to sample from a multinomial, then go with @Asim 's answer.

But the example you give isn't really a multinomial as the result from GetRand (after using the correction below) is just the position of the success category with a sample size of one.

I think the issue is that RandomReal[] is called internally for each element in dist rather than being a single call to RandomReal[]. Therefore, the element selected never gets much past 60 as there are multiple chances to get a low number. (I would bet that's by design rather than considered a bug.) The fix might be the following:

S = 300;
dis = Accumulate@Table[1/S, {k, 1, S}];
GetRand[dist_] := Module[{r},
r = RandomReal[];
FirstPosition[dist, SelectFirst[dist, # >= r &]][[1]]];
test = Table[GetRand[dis], {k, 1, 100000}];
Histogram[test, S, "PDF"]


If you only need the position of what is selected from a sample of size 1, then RandomChoice should be considered:

SeedRandom[12345];
AbsoluteTiming[test = Table[GetRand[dis], {k, 1, 100000}];]
(* {8.72437, Null} *)
SeedRandom[12345];
AbsoluteTiming[RandomChoice[Range[300], 100000];]
(* {0.0012076, Null} *)