3
$\begingroup$

In mathematica, RandomInteger[] generates binary numbers (0 or 1) with equal probablity (uniform distribution).

If I wish to generate the binary numbers with unequal probability or from some probability distribution, let's say Binomial distribution, then how to generate them?

$\endgroup$
0

1 Answer 1

6
$\begingroup$

For example, for 10 binomial numbers of length 5, you could use either BinomialDistribution (as shown by @Kuba in the comments) or RandomChoice

p = 1/10;

SeedRandom[0]

RandomVariate[BinomialDistribution[1, p], {10, 5}]

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

SeedRandom[0]

RandomChoice[{1 - p, p} -> {0, 1}, {10, 5}]

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

The approaches are equivalent

% == %%%

(* True *)
$\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.