+ Reply to Thread
Results 1 to 11 of 11

Thread: Going crazy over R problem:=)

  1. #1
    Points: 18, Level: 1
    Level completed: 35%, Points required for next Level: 32

    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Going crazy over R problem:=)



    Hi all!

    I'm new here! This is my first post, starting out with needing help, awesome right?

    I have a problem that I've looked at for days, without being able to understand
    what's going on. I hope it's ok that I type the problem here:

    It's involving dice(or die)

    We have this function:

    dice <- function(n) {floor(runif(n,1,7)) }

    The 'game' is throwing 5 dice and noting how many 6'es there is.

    S, there are these three codes:

    Code 1:

    amount <- 0
    for (k in 1:5) {if (dice(5)[k]==6) amount <- amount + 1}
    amount(call amount)

    Code 2:

    amount <- 0
    throw <- dice(5)
    for (k in 1:5) {if (throw[k]==6) amount <- amount + 1}
    amount(call amount)

    Code 3:

    throw <- dice(5)
    amount <- sum(slag==6)
    amount(call amount)

    Now, one of these should be wrong. Initially I thought it would be code 2, since
    I thought it would call things twice, so I set for 1 and 3 being correct. But then
    I started doubting that...If code 2 has throw calling dice(5), then it should be the
    same, yes?

    I really can't make heads or tails in this....could someone please explain which one
    is the wrong one, and why that is? I'm going crazy!!

    Thanks for your time!!

    Nicholas

  2. #2
    Probably A Mammal
    Points: 14,428, Level: 77
    Level completed: 95%, Points required for next Level: 22
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    1,949
    Thanks
    221
    Thanked 418 Times in 386 Posts

    Re: Going crazy over R problem:=)

    Do you understand what the dice function is doing? Namely, do you understand what runif(5, 1, 7) returns?

  3. #3
    Probably A Mammal
    Points: 14,428, Level: 77
    Level completed: 95%, Points required for next Level: 22
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    1,949
    Thanks
    221
    Thanked 418 Times in 386 Posts

    Re: Going crazy over R problem:=)

    Though, I don't know why they wouldn't just do

    Code: 
    dice <- function(x) {sample(1:6, x, TRUE)}
    And it's good if you show code to wrap it in code tags, either by using the button for it on the editor or doing it manually like I did above

    [code]dice <- function(x) {sample(1:6, x, TRUE)}[/code]

  4. The Following User Says Thank You to bryangoodrich For This Useful Post:

    trinker (10-20-2012)

  5. #4
    Points: 18, Level: 1
    Level completed: 35%, Points required for next Level: 32

    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Going crazy over R problem:=)

    I'll remember the code signs, thanks!

    The runif(5,1,7) means that it runs 5 times of a random value of 1 through 7, and then floor let it go to whole numbers(down), right?

    Thanks!

  6. #5
    Probably A Mammal
    Points: 14,428, Level: 77
    Level completed: 95%, Points required for next Level: 22
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    1,949
    Thanks
    221
    Thanked 418 Times in 386 Posts

    Re: Going crazy over R problem:=)

    It's like sample but it's drawing those 5 random samples in this case from the uniform distribution between the values 1 and 7. Since it includes fractions, taking the floor will ensure that you get integers 1 through 6.

    Now in all these cases the amount and amount = amount + 1 is just a counter variable to aggregate how many times a 6 occurred. So tell me, what is the difference between codes 1 and 2? What happens on each iteration of the loop in those examples?

  7. #6
    Points: 18, Level: 1
    Level completed: 35%, Points required for next Level: 32

    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Going crazy over R problem:=)

    I hope I don't say anything stupid!

    In code 2, it runs 'throw' five times? And 'throw' runs 'dice' five times?

  8. #7
    Probably A Mammal
    Points: 14,428, Level: 77
    Level completed: 95%, Points required for next Level: 22
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    1,949
    Thanks
    221
    Thanked 418 Times in 386 Posts

    Re: Going crazy over R problem:=)

    Throw isn't a function so you're not "running" it at all. Instead, throw is a vector containing some point values--namely, those values returned from a dice call which is just a wrapper to calling runif. It's their "dice random sampling" so to speak. So do you see the significant difference in what the loop is doing and what values are appearing at each iteration of the loop?

  9. #8
    Points: 18, Level: 1
    Level completed: 35%, Points required for next Level: 32

    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Going crazy over R problem:=)

    Actually, I can't see it at all

    I can't see why Code 2 is different than Code 1, and what difference it would make
    Last edited by AE35; 10-21-2012 at 04:16 PM.

  10. #9
    Probably A Mammal
    Points: 14,428, Level: 77
    Level completed: 95%, Points required for next Level: 22
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    1,949
    Thanks
    221
    Thanked 418 Times in 386 Posts

    Re: Going crazy over R problem:=)

    Well what is

    Code: 
    throw <- dice(5)
    doing? For that matter, what does

    Code: 
    dice(5)[3]
    do? What if you did that 6 different times? Does it matter In fact, run dice(5) several times. What do you see? Is the first expression above within the loop?

  11. #10
    Points: 18, Level: 1
    Level completed: 35%, Points required for next Level: 32

    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Going crazy over R problem:=)

    Yes, throw is within the loop, so it must be looped 5 times, right? And if a throw is = 6, then it adds +1 to the counter.

    ...but throw runs the dice function 5 times, right? Dice(5) should give me the result of five dice/die, right?

    I feel like it is Code 2 that is wrong, but I can't see the punchline

  12. #11
    Probably A Mammal
    Points: 14,428, Level: 77
    Level completed: 95%, Points required for next Level: 22
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    1,949
    Thanks
    221
    Thanked 418 Times in 386 Posts

    Re: Going crazy over R problem:=)


    No. Throw does not run anything. Throw is not a function. You call the variable within the loop to return its value, this is true. But it is assigned values before the loop. That assignment does not change afterwards. Dice(5) returns a random uniform distribution sample of integers 1 through 6.

    Code: 
    print(dice(5))
    dice(5)[3]
    
    set.seed(666)  # The next random stuff will be the same every time when you set the randomization seed
    throw <- dice(5)
    print(throw)
    throw[3]
    See the differences in these vectors?

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts








Advertise on Talk Stats