# Partial Sum of Binary Sequence not Working

I have the following code:

G[0] := RandomInteger[];
Do[
If[
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?

• Why not use RandomChoice[] instead? – J. M. will be back soon May 9 '16 at 17:54
• 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. – N.J.Evans May 9 '16 at 17:58
• 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? – partseq May 9 '16 at 18:00
• Consider editing your question with an example that more closely resembles your actual problem, then (if not the problem itself). – J. M. will be back soon May 9 '16 at 18:08