assigning a variable to sample causing it to return the same value over and over

I am trying to run a simple sim that I have done a million times. simulate three dice and take the sum. For some reason when I am assigning my code to a variable it keeps returning the same value over and over. I have cleared my environment and set.seed is not being used. my code is simple

sim <- sum(sample(1:6,3)

Has anyone else ever had this problem?

Can you provide a full reproducible example of all the code you're running? Also, to simulate rolling three six-sided dice, the code would be sum(sample(1:6, 3, replace=TRUE)).

Thank you for responding, this is it right here

sim <- sum(sample(1:6,3, replace = TRUE))

every time i run sim it returns the same number. Then if i take a break and try again it returns a different number over and over. I cleared history and environment and that is the only thing in my rmd.

Its storing it as a value, do I need to make it a function?

edit.
Ive just seen that you expectation is that you would type sim, and see a different number, but as you say its not a function, so it would just show the stored value
so it seems you want

mysim <- function(){
 sum(sample(1:6,3, replace = TRUE))
}

and then use it like

mysim()
1 Like

Thank you. I am still relatively new to programming in general.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.