7

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!

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
Chen Stats Yu
  • 4,986
  • 2
  • 24
  • 50
  • 1
    RandomChoise[{1,1,1,1,1,1,1,1,1,0}] assuming that RandomChoise has a constant probabality of choosing every element – k_v Apr 04 '15 at 16:22
  • 3
    There is an incongruence between title and content of the question? – enzotib Apr 04 '15 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. – 2012rcampion Apr 04 '15 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! – Chen Stats Yu May 11 '15 at 19:44
  • No problem. I happened to come across it. Sometimes I forget for a while, too. :) – Michael E2 May 11 '15 at 19:45

3 Answers3

26

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

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
kirma
  • 19,056
  • 1
  • 51
  • 93
6
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}]
David G. Stork
  • 41,180
  • 3
  • 34
  • 96
  • "One nice property of RandomChoice is that it can be generalized..." - so too can BernoulliDistribution... – ciao Apr 05 '15 at 01: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? – David G. Stork Apr 05 '15 at 02: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 Apr 05 '15 at 03: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. – David G. Stork Apr 05 '15 at 03: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 Apr 05 '15 at 04:28
  • @rasher Thanks... but wow.... so much more complicated! – David G. Stork Apr 05 '15 at 16:00
5

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}]
Ajasja
  • 13,634
  • 2
  • 46
  • 104