[R] Head or Tails game

arun smartpink111 at yahoo.com
Sat Aug 4 05:53:50 CEST 2012


HI,

You could try this:
set.seed(112)
list1<-vector("list",1000)
for(i in 1:1000){
 list1[[i]]<-sample(c(-1,1),40,replace=TRUE)}
 dat1<-do.call(rbind,lapply(list1,function(x) sum(x)))
dat2<-matrix(dat1,ncol=20,byrow=TRUE)
head(dat2)
 #    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#[1,]   -2   12    0    8    0    4   -8    2   -8    -4   -14     8    -4   -12
#[2,]    8    4   -4    4   12   -2   -6    0   -8     8    -8    12     2     2
#[3,]   -2    8    2   -6  -12   -6    8    6   -4     4     4     8     6    10
#[4,]    6    6    0    4   10   -8   -4   -2    4   -10    -6    -2     4     2
#[5,]   -6   -4   -6   -6    4    0  -14   -2    0    -8    -6     2     4    -8
#[6,]    0   10    0    0  -14   -2    2    0    6   -10     4     0    -4     4
  #   [,15] [,16] [,17] [,18] [,19] [,20]
#[1,]     4    12    -6     4    10     8
#[2,]    -6     0     4     8     0    12
#[3,]    -2    -4    10   -10     6   -10
#[4,]     2     6     4     6     8     6
#[5,]    10     0    -6    -2    10    -8
#[6,]     0   -12    16    -2     2     4


#system time for 10,000 times
system.time({
 set.seed(112)
 list1<-vector("list",10000)
 for(i in 1:10000){
  list1[[i]]<-sample(c(-1,1),40,replace=TRUE)}
  dat1<-do.call(rbind,lapply(list1,function(x) sum(x)))
 dat2<-matrix(dat1,ncol=200,byrow=TRUE)
 })
   user  system elapsed 
  0.112   0.000   0.111 


A.K.




----- Original Message -----
From: darnold <dwarnold45 at suddenlink.net>
To: r-help at r-project.org
Cc: 
Sent: Friday, August 3, 2012 10:14 PM
Subject: Re: [R] Head or Tails game

David,

set.seed(123)  # always good to make reproducible 
winnings <- sum(sample(c(-1,1), 10000, replace=TRUE)) 

Unfortunately, that's not the game. The game requires 40 flips of a coin.

Then you have to play the game 10,000 times.

D.



--
View this message in context: http://r.789695.n4.nabble.com/Head-or-Tails-game-tp4639142p4639145.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list