Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I would like to simulate the evolution of a bankroll of a 2 player game where you have the probability of winning p. If a player wins, the bankroll grows by 1 unit minus a fee that each player pays, let's say the fee is r. So, when a player wins a game the bankroll grows by 1-2*r, since each player pays the fee. If a player loses the game obviously the bankroll diminishes by 1 unit.

In the example below p=0.517 and the fee is such that the player wins 0.959 units. When I generate the plot I am surprised to see that the line decreases while I would expect that it would increase. Am I generating the NestList incorrectly? Thanks.

RandomWalk[n_] := 
   NestList[(# + If[RandomReal[] >= 0.517, 0.959, -1]) &, 0, n]

ListPlot[RandomWalk[3000], Joined -> True
share|improve this question

closed as too localized by Oleksandr R., whuber, Sjoerd C. de Vries, Artes, Yves Klett May 1 '13 at 13:08

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

Although there is nothing wrong with this question, since it rests on a simple misunderstanding, I am going to vote to close it as "too localized". –  Oleksandr R. May 1 '13 at 3:52

2 Answers 2

Not a direct answer to your question, but I've found RandomChoice is generally the most "intentful" of the random functions. With RandomChoice, your code would be something like:

randomWalk[n_, p_: .517] := NestList[# + RandomChoice[{p, 1 - p} -> {.959, -1}] &, 0, n];

And for example, if you wanted a 2-to-1 ratio, you would write:

randomWalk[n_] := NestList[# + RandomChoice[{2, 1} -> {.959, -1}] &, 0, n];

For this problem, notice there is no state involved, so you can generate a random stream independently and then Fold over it:

randomWalk[n_, p_: .517] := FoldList[Plus, 0, RandomChoice[{p, 1 - p} -> {.959, -1}, n]];

And if you really want to impress the girls:

randomWalk[n_, p_: .517] := Accumulate@RandomChoice[{p, 1 - p} -> {.959, -1}, n];
share|improve this answer
(+1) But why impress only the girls? –  Jens Apr 28 '13 at 5:07

You just have to turn the inequality around:

RandomWalk[n_] := 
 NestList[(# + If[RandomReal[] <= 0.517, 0.959, -1]) &, 0, n]

ListPlot[RandomWalk[3000], Joined -> True]

enter image description here

The probability .517 is supposed to mean that the first argument of the If gets executed. Since the interval of RandomReal goes from 0 to 1, the part of the interval below p has the (length) measure given by p.

share|improve this answer
oh right! thanks. –  dabd Apr 27 '13 at 22:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.