0
$\begingroup$

I have the following problem: I generated two random numbers

y = RandomVariate[BernoulliDistribution[.5], 2]
s = RandomVariate[ExponentialDistribution[.1], 2]

l = y.s

I want to generate in a loop 100 times the product l but I do not know how

Table[l, {i, 1, 100}]

It means that $y$ and $s$ should be generated 100 times each.

$\endgroup$
5
  • $\begingroup$ Try l := y.s and report back. $\endgroup$ Commented Feb 21, 2016 at 19:10
  • $\begingroup$ Table[RandomVariate[BernoulliDistribution[.5], 2].RandomVariate[ExponentialDistribution[.1], 2],{100}] $\endgroup$ Commented Feb 21, 2016 at 19:10
  • $\begingroup$ @J.M. thanks but unfortunately it does not work $\endgroup$
    – maniA
    Commented Feb 21, 2016 at 19:18
  • $\begingroup$ @DavidG.Stork I know that but I would have it in a more "dynamical" way I would lave such a way that every time when I call l it generates a pair again! Thanks any way $\endgroup$
    – maniA
    Commented Feb 21, 2016 at 19:22
  • $\begingroup$ @maniA: Try running my code several times.... $\endgroup$ Commented Feb 21, 2016 at 19:23

2 Answers 2

5
$\begingroup$

You need to make sure that the definitions of $y$, $s$, and $l$ are re-evaluated every time you invoke them, so new random numbers are generated every time.

Use SetDelayed (:=) in their definition to accomplish that:

Clear[y, s, l]
y := RandomVariate[BernoulliDistribution[.5], 2]
s := RandomVariate[ExponentialDistribution[.1], 2]
l := y.s

Table[l, {100}]

{10.7424, 7.25643, 1.79782, 0., 0., 0., 0., 6.39046, 19.2634, 0., 3.64775, 42.3379, 29.0361, 2.55506, 1.88215, 10.3919, 8.3307, 15.2387, 0., 26.3855, 9.85697, 0., 7.19747, 13., 23.0351, 11.9228, 13.1692, 13.9306, 20.5389, 5.8673, 0., 1.19822, 19.6213, 11.4207, 4.82677, 5.45082, 0.695063, 5.96605, 5.7171, 16.1679, 32.8879, 29.0086, 25.1448, 0., 4.4569, 34.8751, 30.3961, 19.9085, 11.085, 1.0612, 1.35964, 1.46275, 9.38037, 0., 7.50795, 1.98857, 0., 28.8082, 37.2276, 0., 0., 0., 6.14201, 34.8584, 0., 3.46096, 6.27206, 73.3928, 76.495, 1.91647, 13.1031, 30.1474, 1.64898, 0., 0., 31.3811, 0.0756431, 0., 20.0724, 11.7921, 5.37092, 22.8973, 10.3185, 4.998, 5.84697, 0.537638, 14.6865, 17.3023, 11.0843, 0., 3.27186, 0., 3.34524, 11.12, 16.9808, 2.14918, 14.7753, 4.76471, 0., 3.78329}

$\endgroup$
6
$\begingroup$

Apart from the straightforward approach in Marco's answer, you can instead derive a TransformedDistribution[] that can be used within RandomVariate[]:

bd = BernoulliDistribution[1/2]; ed = ExponentialDistribution[1/10];
td = TransformedDistribution[b1 e1 + b2 e2,
                             {b1 \[Distributed] bd, b2 \[Distributed] bd,
                              e1 \[Distributed] ed, e2 \[Distributed] ed}];

and then do RandomVariate[td, 100].

$\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.