Re: easy question about random numbers
- To: mathgroup at smc.vnet.net
- Subject: [mg53391] Re: easy question about random numbers
- From: "Steve Luttrell" <steve_usenet at _removemefirst_luttrell.org.uk>
- Date: Sun, 9 Jan 2005 23:03:44 -0500 (EST)
- References: <crqb7f$cak$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
You could use MultinomialDistribution. Here is an example:
<<Statistics`MultiDiscreteDistributions`
k = 10;
p = (#1/Total[#1] & )[Table[Random[], {k}]];
n = 1;
dist = MultinomialDistribution[n, p];
k is the number of states, p is the vector of state probabilities (in this
case these are random numbers that sum to unity), and n is the number of
trials.
You can then use Random[dist] to generate a histogram with k bins, and with
the n trials placed in the appropriate bins. In this case (i.e. n=1) there
is a 1 in one of the bins, and the rest of the bins contain 0.
If you use RandomArray[dist, m] you can generate the result of m evaluations
of Random[dist].
Alternatively, if you don't mind lumping all of the results together in a
single histogram (this wipes out knowledge of the order in which the trials
occurred) you could set n = m in the first place, and then evaluate
Random[dist] once. Here is how this works for n = 100:
k = 10;
p = (#1/Total[#1] & )[Table[Random[], {k}]];
n = 100;
dist = MultinomialDistribution[n, p];
Random[dist]
Steve Luttrell
"Pedrito" <pedrito6 at softhome.net> wrote in message
news:crqb7f$cak$1 at smc.vnet.net...
> Hi everybody!
>
> I wanted to obtain a discrete random number generator that I needed for
> a project.
>
> On the library Statistics`DiscreteDistributions` I could find the
> DiscreteUniformDistribution
> function. But I wanted to specify the probability for each one of the
> states.
>
>
> For instance:
> If we need to simulate an unfair dice, we could have this probabilities
> for each one of the sides:
> {1/6, 1/6, 1/6, 1/6, 9/60, 11/60}
>
> So I wrote:
> li2 = {1/6, 1/6, 1/6, 1/6, 9/60, 11/60}
> li3=FoldList[Plus,0,li2]
> Module[{i = 1, r = Random[]}, While[ !li3[[i]] < r < li3[[i + 1]], i++];
> i]
>
> It works ok but I don't know if there is another (better) way of doing
> this.
> Any suggestion?
>