1
$\begingroup$

I wrote the code below and I wish to run it 100 times, store the results for j1 and j2 in two vectors v1 and v2. After this, I want to plot {i, v1[i]} and {i, v2[i]} together on the same plot. Can somebody help me please?

zar1 := Random[Integer, {1, 6}];
zar2 := Random[Integer, {1, 6}];
c := 0
j1 := 0
j2 := 0
For[i = 0, i < 10000, i++,
  While[zar1 + zar2 != 11, c = c + 1]
    If[Mod[c, 2] == 0, j2 = j2 + 1, j1 = j1 + 1]];
N[j1/10000]
N[j2/10000]
$\endgroup$
3
  • 2
    $\begingroup$ What are zar1 and zar2? $\endgroup$
    – Mr.Wizard
    Feb 1, 2017 at 23:18
  • $\begingroup$ two ordinary dies; sorry I forget to put the first 2 lines of the code zar1 := Random[Integer, {1, 6}] zar2 := Random[Integer, {1, 6}] $\endgroup$
    – Andrew
    Feb 1, 2017 at 23:33
  • $\begingroup$ Actually, this comes from a simple probability problem: J1 and J2 play a die game until the one who hits 11 (sum of zar1 and zar 2) is declared thewinner. Who has the best probability to win the game if J1 starts the game? $\endgroup$
    – Andrew
    Feb 1, 2017 at 23:39

3 Answers 3

2
$\begingroup$

Here is how I would do it. I break the problem into three parts as follows:

  1. Command to play one game and return 1 when the game ends on an odd throw and 0 otherwise.

    game :=
      Module[{throws = 0},
        While[Total[RandomInteger[6, 2]] != 11, throws++];
        Boole[OddQ[throws]]
    
  2. Function to estimate, after playing a specified number of games, the probabilities that a game will end at an odd or even number of throws.

    gameSequence[games_] :=
      Module[{pOdd = N @ Total[Table[game, games]/games]}, {pOdd, 1 - pOdd}]
    
  3. Function to run a specified number of game sequences and gather statistics, $P_{\rm odd}$ and $P_{\rm even}$ for plotting.

    stats[seqs_Integer /; seqs > 0, games_Integer /; games > 0] :=
      Table[gameSequence[games], seqs]
    

Now I get 50 samples of sequences of 1000 games each.

SeedRandom[42]; data = Transpose[stats[50, 1000]];

Here is a plot of results.

ListPlot[data,
  PlotStyle -> {AbsolutePointSize[6]},
  PlotLegends -> SwatchLegend[Automatic, {Subscript[P, odd], Subscript[P, even]}]]

plot

$\endgroup$
2
  • $\begingroup$ Something is wrong since the distribution of the odd is symmetric with even!!!!!!! Thanks for the post! $\endgroup$
    – Andrew
    Feb 2, 2017 at 20:11
  • $\begingroup$ @Andrew. Nothing is wrong. The symmetry comes from $P_{\rm odd} + P_{\rm even} = 1$, since every trail must end with at an odd count or an even count. $\endgroup$
    – m_goldberg
    Feb 3, 2017 at 4:19
1
$\begingroup$

I think this is what you want:

zar1 := Random[Integer, {1, 6}];
zar2 := Random[Integer, {1, 6}];
c = 0;
j1 = 0;
j2 = 0;

Create a table with i in the first column, j1 in the second column and j2 in the third column:

result = Table[While[zar1 + zar2 != 11, c += 1]; 
   If[Mod[c, 2] == 0, j2 += 1, j1 += 1]; {i, j1, j2}, {i, 1, 10000}];

Plot the result:

ListLinePlot[{result[[All, {1, 2}]], result[[All, {1, 3}]]}]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thank you for the answer but this is not what I need. $\endgroup$
    – Andrew
    Feb 1, 2017 at 23:58
  • $\begingroup$ I need to store the result for j1 and j2 in two vectors v1 and v2 (those are the results of running the code written in the post), repeat this 100 times and create v1 and v2 with 100 elements each. Then plot the distributions {i, v1[i]} and {i, v2[i]} in the same graph but separate. $\endgroup$
    – Andrew
    Feb 2, 2017 at 0:01
  • $\begingroup$ Just call v1=result[[All,2]] and v2=result[[All,3]], then create a table of vectors using the same syntax that I've shown you. $\endgroup$
    – Felix
    Feb 2, 2017 at 0:44
0
$\begingroup$

Here is something that might work for you. I tried to reuse as much as possible you original code.

zar1 := Random[Integer, {1, 6}];
zar2 := Random[Integer, {1, 6}];
j1j2 := Module[{c = 0, j1 = 0, j2 = 0, i},
  For[i = 0, i < 10000, i++, 
   While[zar1 + zar2 != 11, c = c + 1]
      If[Mod[c, 2] == 0, j2 = j2 + 1, j1 = j1 + 1]];
  {j1, j2}]
SeedRandom[0];
DiscretePlot[j1j2, {i, 1, 100}, Filling -> None, 
 PlotStyle -> {AbsolutePointSize[6]}]

Mathematica graphics

(sorry I could not find the option to have two colors for j1 and j2).

Btw, as $j1+j2=1000$ you may want to plot just one of them. Here is j1:

DiscretePlot[First@j1j2, {i, 1, 100}, Filling -> None, 
 PlotStyle -> {AbsolutePointSize[6]}]

Mathematica graphics

If you need the vector of results for other purposes you can obtain is like this:

SeedRandom[0];
list = Table[j1j2, {i, 1, 100}];
list1 = Table[{i, First@list[[i]]}, {i, 1, 100}];
list2 = Table[{i, Last@list[[i]]}, {i, 1, 100}];
ListPlot[{list1, list2}, PlotStyle -> {AbsolutePointSize[6]}]

Mathematica graphics

$\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.