Return average number of turns over 1000 times
I'm relatively new to R and I've been set a project to simulate a game. I've designed the game and entered a code in which it will return the number of turns taken to win the game, however I'm stuck with how to simulate the game 1000 times and return the average, although it seems so simple, I just can't work it out.
The code to return how many turns it takes to win:
s=seq(1:25)
current=vector()
current[1]=1
gameon=1
turns=1
while(gameon==1)
current[turns+1]=sample(s,1,replace=TRUE,prob=w[current[turns],])
turns=turns+1
if (current[turns]==25)
gameon=0
The game code and transition matrix:
w = matrix(0, nrow=25, ncol=25)
for (i in 1:25) #RED - 1,2 down 2 3,4 back 3 5,6
stay
#AMBER - 1,2 down 1 3,4 back 2 5,6
forward 1
if (i == 4) #GREEN - 1,2 stay 3,4 forward 1 5,6
forward 2
w[i, i+2] = 1/3
w[i,i+1] = 1/3 # effects on a state with a GREEN pub
w[i,i] = 1/3
else if (i == 6)
w[i,i] = 1/3
w[i,i+1] = 1/3
w[i,i+2] = 1/3
else if (i == 7)
w[i,i-3] = 1/3
w[i,i-2] = 1/3 # effects on a state with an AMBER pub
w[i,i+1] = 1/3
else if (i == 12)
w[i,i] = 1/3
w[i,i+1] = 1/3
w[i,i+2] = 1/3
else if (i == 16)
w[i,i-3] = 1/3
w[i,i-2] = 1/3 # effects on a state with a RED pub
w[i,i+1] = 1/3
else if (i == 15)
w[i,i-10] = 1/3
w[i,i-3] = 1/3 # effects on a state with a RED pub
w[i,i] = 1/3
else if (i == 18)
w[i,i] = 1/3
w[i,i+1] = 1/3 # effects on a state with a RED pub
w[i,i+2] = 1/3
else if (i == 21)
w[i,i-10] = 1/3
w[i,i-3] = 1/3
w[i,i] = 1/3
else if (i == 24)
w[i,i-10] = 1/3
w[i,i-3] = 1/3 #horrible square to be stuck on, cant win from
this square.
w[i,i] = 1/3
else if (i <= 19)
w[i, seq(i+1,i+6)] = 1/6
else if (i==20)
w[i,seq(i+1,i+4)] = 1/6
w[i,i+5] = 2/6
else if (i==22)
w[i,seq(i+1,i+2)] = 1/6
w[i,i+3] = 4/6
else if (i == 23)
w[i,i+1] = 1/6
w[i,i+2] = 5/6
else if (i == 25)
w[i,i] = 1 #game finished
I believe a for loop would be best to use but I don't know how to set it up to return the average number of turns.
r for-loop simulation markov-chains
add a comment |
I'm relatively new to R and I've been set a project to simulate a game. I've designed the game and entered a code in which it will return the number of turns taken to win the game, however I'm stuck with how to simulate the game 1000 times and return the average, although it seems so simple, I just can't work it out.
The code to return how many turns it takes to win:
s=seq(1:25)
current=vector()
current[1]=1
gameon=1
turns=1
while(gameon==1)
current[turns+1]=sample(s,1,replace=TRUE,prob=w[current[turns],])
turns=turns+1
if (current[turns]==25)
gameon=0
The game code and transition matrix:
w = matrix(0, nrow=25, ncol=25)
for (i in 1:25) #RED - 1,2 down 2 3,4 back 3 5,6
stay
#AMBER - 1,2 down 1 3,4 back 2 5,6
forward 1
if (i == 4) #GREEN - 1,2 stay 3,4 forward 1 5,6
forward 2
w[i, i+2] = 1/3
w[i,i+1] = 1/3 # effects on a state with a GREEN pub
w[i,i] = 1/3
else if (i == 6)
w[i,i] = 1/3
w[i,i+1] = 1/3
w[i,i+2] = 1/3
else if (i == 7)
w[i,i-3] = 1/3
w[i,i-2] = 1/3 # effects on a state with an AMBER pub
w[i,i+1] = 1/3
else if (i == 12)
w[i,i] = 1/3
w[i,i+1] = 1/3
w[i,i+2] = 1/3
else if (i == 16)
w[i,i-3] = 1/3
w[i,i-2] = 1/3 # effects on a state with a RED pub
w[i,i+1] = 1/3
else if (i == 15)
w[i,i-10] = 1/3
w[i,i-3] = 1/3 # effects on a state with a RED pub
w[i,i] = 1/3
else if (i == 18)
w[i,i] = 1/3
w[i,i+1] = 1/3 # effects on a state with a RED pub
w[i,i+2] = 1/3
else if (i == 21)
w[i,i-10] = 1/3
w[i,i-3] = 1/3
w[i,i] = 1/3
else if (i == 24)
w[i,i-10] = 1/3
w[i,i-3] = 1/3 #horrible square to be stuck on, cant win from
this square.
w[i,i] = 1/3
else if (i <= 19)
w[i, seq(i+1,i+6)] = 1/6
else if (i==20)
w[i,seq(i+1,i+4)] = 1/6
w[i,i+5] = 2/6
else if (i==22)
w[i,seq(i+1,i+2)] = 1/6
w[i,i+3] = 4/6
else if (i == 23)
w[i,i+1] = 1/6
w[i,i+2] = 5/6
else if (i == 25)
w[i,i] = 1 #game finished
I believe a for loop would be best to use but I don't know how to set it up to return the average number of turns.
r for-loop simulation markov-chains
add a comment |
I'm relatively new to R and I've been set a project to simulate a game. I've designed the game and entered a code in which it will return the number of turns taken to win the game, however I'm stuck with how to simulate the game 1000 times and return the average, although it seems so simple, I just can't work it out.
The code to return how many turns it takes to win:
s=seq(1:25)
current=vector()
current[1]=1
gameon=1
turns=1
while(gameon==1)
current[turns+1]=sample(s,1,replace=TRUE,prob=w[current[turns],])
turns=turns+1
if (current[turns]==25)
gameon=0
The game code and transition matrix:
w = matrix(0, nrow=25, ncol=25)
for (i in 1:25) #RED - 1,2 down 2 3,4 back 3 5,6
stay
#AMBER - 1,2 down 1 3,4 back 2 5,6
forward 1
if (i == 4) #GREEN - 1,2 stay 3,4 forward 1 5,6
forward 2
w[i, i+2] = 1/3
w[i,i+1] = 1/3 # effects on a state with a GREEN pub
w[i,i] = 1/3
else if (i == 6)
w[i,i] = 1/3
w[i,i+1] = 1/3
w[i,i+2] = 1/3
else if (i == 7)
w[i,i-3] = 1/3
w[i,i-2] = 1/3 # effects on a state with an AMBER pub
w[i,i+1] = 1/3
else if (i == 12)
w[i,i] = 1/3
w[i,i+1] = 1/3
w[i,i+2] = 1/3
else if (i == 16)
w[i,i-3] = 1/3
w[i,i-2] = 1/3 # effects on a state with a RED pub
w[i,i+1] = 1/3
else if (i == 15)
w[i,i-10] = 1/3
w[i,i-3] = 1/3 # effects on a state with a RED pub
w[i,i] = 1/3
else if (i == 18)
w[i,i] = 1/3
w[i,i+1] = 1/3 # effects on a state with a RED pub
w[i,i+2] = 1/3
else if (i == 21)
w[i,i-10] = 1/3
w[i,i-3] = 1/3
w[i,i] = 1/3
else if (i == 24)
w[i,i-10] = 1/3
w[i,i-3] = 1/3 #horrible square to be stuck on, cant win from
this square.
w[i,i] = 1/3
else if (i <= 19)
w[i, seq(i+1,i+6)] = 1/6
else if (i==20)
w[i,seq(i+1,i+4)] = 1/6
w[i,i+5] = 2/6
else if (i==22)
w[i,seq(i+1,i+2)] = 1/6
w[i,i+3] = 4/6
else if (i == 23)
w[i,i+1] = 1/6
w[i,i+2] = 5/6
else if (i == 25)
w[i,i] = 1 #game finished
I believe a for loop would be best to use but I don't know how to set it up to return the average number of turns.
r for-loop simulation markov-chains
I'm relatively new to R and I've been set a project to simulate a game. I've designed the game and entered a code in which it will return the number of turns taken to win the game, however I'm stuck with how to simulate the game 1000 times and return the average, although it seems so simple, I just can't work it out.
The code to return how many turns it takes to win:
s=seq(1:25)
current=vector()
current[1]=1
gameon=1
turns=1
while(gameon==1)
current[turns+1]=sample(s,1,replace=TRUE,prob=w[current[turns],])
turns=turns+1
if (current[turns]==25)
gameon=0
The game code and transition matrix:
w = matrix(0, nrow=25, ncol=25)
for (i in 1:25) #RED - 1,2 down 2 3,4 back 3 5,6
stay
#AMBER - 1,2 down 1 3,4 back 2 5,6
forward 1
if (i == 4) #GREEN - 1,2 stay 3,4 forward 1 5,6
forward 2
w[i, i+2] = 1/3
w[i,i+1] = 1/3 # effects on a state with a GREEN pub
w[i,i] = 1/3
else if (i == 6)
w[i,i] = 1/3
w[i,i+1] = 1/3
w[i,i+2] = 1/3
else if (i == 7)
w[i,i-3] = 1/3
w[i,i-2] = 1/3 # effects on a state with an AMBER pub
w[i,i+1] = 1/3
else if (i == 12)
w[i,i] = 1/3
w[i,i+1] = 1/3
w[i,i+2] = 1/3
else if (i == 16)
w[i,i-3] = 1/3
w[i,i-2] = 1/3 # effects on a state with a RED pub
w[i,i+1] = 1/3
else if (i == 15)
w[i,i-10] = 1/3
w[i,i-3] = 1/3 # effects on a state with a RED pub
w[i,i] = 1/3
else if (i == 18)
w[i,i] = 1/3
w[i,i+1] = 1/3 # effects on a state with a RED pub
w[i,i+2] = 1/3
else if (i == 21)
w[i,i-10] = 1/3
w[i,i-3] = 1/3
w[i,i] = 1/3
else if (i == 24)
w[i,i-10] = 1/3
w[i,i-3] = 1/3 #horrible square to be stuck on, cant win from
this square.
w[i,i] = 1/3
else if (i <= 19)
w[i, seq(i+1,i+6)] = 1/6
else if (i==20)
w[i,seq(i+1,i+4)] = 1/6
w[i,i+5] = 2/6
else if (i==22)
w[i,seq(i+1,i+2)] = 1/6
w[i,i+3] = 4/6
else if (i == 23)
w[i,i+1] = 1/6
w[i,i+2] = 5/6
else if (i == 25)
w[i,i] = 1 #game finished
I believe a for loop would be best to use but I don't know how to set it up to return the average number of turns.
r for-loop simulation markov-chains
r for-loop simulation markov-chains
edited Nov 12 at 20:19
benvc
3,7671319
3,7671319
asked Nov 12 at 19:35
F. Wotton
254
254
add a comment |
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268948%2freturn-average-number-of-turns-over-1000-times%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268948%2freturn-average-number-of-turns-over-1000-times%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown