I have the following code:

G[0] := RandomInteger[];
  RandomReal[] < 0.3,
  G[i] = 1,
  G[i] = 0
 {i, 1, 50}]
Table[Sum[G[j], {j, 0, i}], {i, 0, 50}]

The first entry of G is either 0 or 1. The following entries of G have a probability 0.3 of being a 1. This generates a sequence that looks right. However, on the last line where I ask it to give me a table with partial sums, it doesn't do it. What could be wrong?

  • $\begingroup$ Why not use RandomChoice[] instead? $\endgroup$ – J. M.'s technical difficulties May 9 '16 at 17:54
  • $\begingroup$ As J.M. said, RandomInteger[] + Accumulate@RandomChoice[{0.3, 0.7} -> {1, 0}, 50] gives you partial sums for a binary list generated according to your weights. $\endgroup$ – N.J.Evans May 9 '16 at 17:58
  • $\begingroup$ For this example I simplified the probability to 0.3 for the purposes of asking the question. In what I am actually working on the probability comes from a function calculated at every stage. Would RandomChoice[] work with a function inside? $\endgroup$ – partseq May 9 '16 at 18:00
  • $\begingroup$ Consider editing your question with an example that more closely resembles your actual problem, then (if not the problem itself). $\endgroup$ – J. M.'s technical difficulties May 9 '16 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.