# Can I sample from a distribution whose parameters depend on the current iteration?

Let's say I want to sample from 100 Beta distributions, and in each successive one, the first parameter increases by 1. So given a current iteration number n, I want to make a list of

RandomVariate[ BetaDistribution[n, b] ]


for all n values from 1 to 100. Is there an easy way to do this in Mathematica? I assume I would be able to find a way without needing to define 100 different distributions.

Edit 2: I changed the distribution I was talking about slightly--I'm asking this as a general question, the actual distribution I'm trying to sample from is a little more complicated but I'm hoping that if there's a solution that works for this, I can apply that solution elsewhere.

Edit: I thought this was possible with a For loop but after trying it out looks like I was wrong. Any help is appreciated

• RandomVariate[NormalDistribution[0, sv], 100] + Range@100...
– ciao
Apr 1, 2020 at 6:54
• @ciao Sorry, I'll edit my OP, I forgot that Normal distributions can just be shifted like that. What I'm asking is if there's a more general way to do this--I want to be able to apply this to, say, the standard deviation instead of the mean, or to another distribution altogether. My example was just a simple example of what I'm looking for. Apr 1, 2020 at 7:08

I found what I was looking for. The example that I gave can be done (if we want to do it in one line) with:

RandomVariate[#]&/@(BetaDistribution[#,b]&/@Range);


If we wanted to do the same thing for both variables, we could do something instead like

RandomVariate[#]&/@(MapThread[BetaDistribution[#1,#2]&,{Range,Range}]);


This seems pretty slow right now for what I'm using it for, so if anyone has any optimization suggestions let me know!

• RandomVariate[BetaDistribution[#, b], 1][] & /@ Range is about 6 times faster (although neither approach seems slow).
– JimB
Apr 1, 2020 at 15:00
• @JimB Thanks! It looks like the slowness was because I was using CategoricalDistribution in my code where BernoulliDistribution was just as useful and also faster. Apr 23, 2020 at 22:40