宝くじがどのくらい当たるか実感してみよう
米国の宝くじ、パワーボールの賞金が積み上がって4億ドルに達し話題になっている。
同僚が「何か一般向けに面白いことを書いてくれ」というので、以下の様に返答してみた。
----
Hi all,
Sorry but statistians' job is rather to discourage people to play Powerball
-- people do not have a good sense of small probabilities.
One day my 7-year old daughter said "I want to play Powerball,"
so I told her "you could never win". But she insisted "I will win!".
Then I created a "Poweball simulator" in the R language. You input
numbers for a ticket, and the result will appear on the screen.
She tried it many many times and finally got $4 (*1). After that, she never
says she wants to play Powerball.
Instruction:
1) Install R language via comprehensive R archive network:
http://cran.r-project.org/
R is available for free on Windows, MacOSX and Linux.
2) Launch R.
3) Cut and paste the attached code in R.
4) Type "powerball()" in R, then you will be prompted to enter numbers you choose.
In this way, we can promote statistical languages rather than Powerball!
Best,
Willy from Montreal
### powerball simulator
powerball <- function(){
YA <- readline("Enter 5 different numbers between 1 and 59 separated by comma:
(e.g. 1, 15, 7, 9, 20):")
YA <- as.numeric(unlist(strsplit(YA, ",")))
if (length(YA)!=5){"Enter 5 numbers!"} else{
YB <- readline("Enter a number between 1 and 35 (e.g. 33):")
YB <- as.numeric(unlist(strsplit(YB, ",")))
if (length(YB)!=1){"Enter only 1 number"} else{
A <- sort(sample(1:59,5))
B <- sample(1:35,1)
TSN <- paste(
"The winning numbers are",
A[1], ",", A[2], ",", A[3], ",", A[4], ",", A[5],
"-", B, ".")
print(TSN)
MQ <- length(intersect(YA,A))
MP <- length(intersect(YB,B))
if (MP ==0){
PZ <- switch(as.character(MQ),
"3" = "You won 7 dollars!",
"4" = "You won 100 dollars!",
"5" = "You won 1,000,000 dollars!",
"You lost.")
}
if (MP ==1){
PZ <- switch(as.character(MQ),
"0" = "You won 4 dollars!",
"1" = "You won 4 dollars!",
"2" = "You won 7 dollars!",
"3" = "You won 100 dollars!",
"4" = "You won 10,000 dollars!",
"5" = "Jackpot!!! You won 400,000,000 dollars!
(or a smaller amount if multiple winners)",
"You lost.")
}
print(PZ)
}
}
}
(*1) パワーボールは1枚2ドル。
同僚が「何か一般向けに面白いことを書いてくれ」というので、以下の様に返答してみた。
----
Hi all,
Sorry but statistians' job is rather to discourage people to play Powerball
-- people do not have a good sense of small probabilities.
One day my 7-year old daughter said "I want to play Powerball,"
so I told her "you could never win". But she insisted "I will win!".
Then I created a "Poweball simulator" in the R language. You input
numbers for a ticket, and the result will appear on the screen.
She tried it many many times and finally got $4 (*1). After that, she never
says she wants to play Powerball.
Instruction:
1) Install R language via comprehensive R archive network:
http://cran.r-project.org/
R is available for free on Windows, MacOSX and Linux.
2) Launch R.
3) Cut and paste the attached code in R.
4) Type "powerball()" in R, then you will be prompted to enter numbers you choose.
In this way, we can promote statistical languages rather than Powerball!
Best,
Willy from Montreal
### powerball simulator
powerball <- function(){
YA <- readline("Enter 5 different numbers between 1 and 59 separated by comma:
(e.g. 1, 15, 7, 9, 20):")
YA <- as.numeric(unlist(strsplit(YA, ",")))
if (length(YA)!=5){"Enter 5 numbers!"} else{
YB <- readline("Enter a number between 1 and 35 (e.g. 33):")
YB <- as.numeric(unlist(strsplit(YB, ",")))
if (length(YB)!=1){"Enter only 1 number"} else{
A <- sort(sample(1:59,5))
B <- sample(1:35,1)
TSN <- paste(
"The winning numbers are",
A[1], ",", A[2], ",", A[3], ",", A[4], ",", A[5],
"-", B, ".")
print(TSN)
MQ <- length(intersect(YA,A))
MP <- length(intersect(YB,B))
if (MP ==0){
PZ <- switch(as.character(MQ),
"3" = "You won 7 dollars!",
"4" = "You won 100 dollars!",
"5" = "You won 1,000,000 dollars!",
"You lost.")
}
if (MP ==1){
PZ <- switch(as.character(MQ),
"0" = "You won 4 dollars!",
"1" = "You won 4 dollars!",
"2" = "You won 7 dollars!",
"3" = "You won 100 dollars!",
"4" = "You won 10,000 dollars!",
"5" = "Jackpot!!! You won 400,000,000 dollars!
(or a smaller amount if multiple winners)",
"You lost.")
}
print(PZ)
}
}
}
(*1) パワーボールは1枚2ドル。
スポンサーサイト