Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Bhargav-Rao
Created April 14, 2015 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bhargav-Rao/a5c56cbf9eda5071d96a to your computer and use it in GitHub Desktop.
Save Bhargav-Rao/a5c56cbf9eda5071d96a to your computer and use it in GitHub Desktop.
fname <- "Data.txt"
if (file.exists(fname))
{
file.remove(fname)
}
for (i in 1:1000)
{
toss <- sample(0:1,3,replace = TRUE)
len <- length(toss[toss[]==1])
write(len,file = fname,append = TRUE)
}
count <- 0
lines <- readLines(file(fname,open = "r"))
for (i in 1:length(lines))
{
if (strtoi(lines[i],base = 10) > 0)
{
count <- count + 1
}
}
percent <- count/10;
theoretical_value <- (1/2 + 1/4 + 1/8)*100
difference <- (theoretical_value-percent)
change <- difference/theoretical_value
print(change)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment