# How to define a distribution that 90% of the time gives me 0?

This maybe a simple question, but I am just stuck with it. I want to do some simulation, say with 0.9 probability, I get a 1, and 0.1 probability get a 0.

How would I do that? Where should I start?

Thanks!

• RandomChoise[{1,1,1,1,1,1,1,1,1,0}] assuming that RandomChoise has a constant probabality of choosing every element
– k_v
Commented Apr 4, 2015 at 16:22
• There is an incongruence between title and content of the question? Commented Apr 4, 2015 at 16:27
• @k_v Try RandomChoice[{0.1, 0.9} -> {0, 1}, n] to do the weighting instead, it's much more flexible this way. Commented Apr 4, 2015 at 22:34
• @MichaelE2 Sorry that I forgot to accept it after a long time! I used to accept answers pretty quickly, but I was suggested that perhaps I should have waited for more people to have a go with it. This is case, I simply forgot to come back and accept it! Commented May 11, 2015 at 19:44
• No problem. I happened to come across it. Sometimes I forget for a while, too. :) Commented May 11, 2015 at 19:45

BernoulliDistribution is a perfect fit for this.

RandomVariate[BernoulliDistribution[1 - 0.1], {50}]


{1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1}

Also, as kguler states, you can use RandomChoice, but the benefit of BernoulliDistribution is that you can operate it also as an abstract distribution, not only a source of randomness. For instance, you can compute its symbolic variance:

Variance[BernoulliDistribution[1 - 1/n]]


(1 - 1/n)/n

• RandomChoice does have the benefit of being compilable, unlike BernoulliDistribution[]. Commented Apr 4, 2015 at 22:29
• @MichaelE2 Are you sure? f = Compile[{{n, _Integer}}, RandomChoice[{.9, .1} -> {1, 0}, n]] seems to compile for me, with the function RandomChoiceWeights in the result of CompilePrint Commented May 11, 2015 at 19:20
• @blochwave, Oops, my bad. I forgot the _Integer on that one but not the others. :/ Now the compiled version is faster until about 10^4 or 10^5. Sorry for the noise. Commented May 11, 2015 at 19:36
• No problem! Admittedly performance tails off for large samples... Commented May 11, 2015 at 19:57
RandomChoice[{.9, .1} -> {1, 0}, 10]


(* {0, 1, 1, 1, 1, 1, 1, 0, 1, 1} *)

Timing results

• Timing[RandomVariate[BernoulliDistribution[.9], {10^8}];] (* {3.38014, Null} *)
• Timing[RandomChoice[{.9, .1} -> {1, 0}, 10^8];](* {5.64937, Null} *)
• dist[] := If[RandomReal[] > 0.9, 0, 1]; Timing[Table[dist[], {i, 10^8}];] (* {13.9842, Null} *)

One nice property of RandomChoice is that it can be generalized, for instance:

RandomChoice[{.7, .2, .1}-> {0, 1, 2}]

• "One nice property of RandomChoice is that it can be generalized..." - so too can BernoulliDistribution...
– ciao
Commented Apr 5, 2015 at 1:43
• @rasher How can the BernoulliDistribution be generalized to give the same as RandomChoice[{.3, .2, .4, .05, .05}-> {-2, 5, 3, 2, "A"}] naturally, i.e., without multiple calls? Commented Apr 5, 2015 at 2:28
• Multiple Bernoulli is just Multinomial, hence, with one call to multinomial, this gives same kind of results as RandomChoice: RandomSample@ Flatten@MapThread[ ConstantArray[#1, #2] &, {#3, RandomVariate[MultinomialDistribution[#1, #2]]}] & given count, probs. and returned values... no real reason to do so, but it is one call to a distribution ;=)
– ciao
Commented Apr 5, 2015 at 3:47
• @rasher Could you give a working example, with actual values (as I did)? For instance, I couldn't see how to get your example to work with five probabilities and five variables. Thanks. Commented Apr 5, 2015 at 3:54
• Sure, e.g. RandomSample@ Flatten@MapThread[ ConstantArray[#1, #2] &, {#3, RandomVariate[ MultinomialDistribution[#1, #2]]}] &[20, {.3, .2, .4, .05, \ .05}, {-2, 5, 3, 2, "A"}] gives same kind of result as RandomChoice[{.3, .2, .4, .05, .05}-> {-2, 5, 3, 2, "A"},20]. But again, no real reason to do this (I suppose if one were generating the dist. probs. programmatically and wanted to use probabilistic functions against them also this has a place) - just some gentle leg-pulling...
– ciao
Commented Apr 5, 2015 at 4:28

Take a uniform random distribution and check if it is above some threshold (0.9 in your case).

For example:

dist[] := If[RandomReal[] > 0.9, 0, 1];
Table[dist[], {i, 100}]

• No need for the i in the iterator. Commented Apr 4, 2015 at 21:50