La generalización de un caso descrito por Dilip Sarwate
Algunos de los métodos descritos en las otras respuestas el uso de un esquema en el que usted lanza una secuencia de $n$ monedas en un "turno" y dependiendo del resultado que elija un número entre 1 o 7 o descartar la vuelta y tirar de nuevo.
El truco es encontrar en la expansión de las posibilidades de un múltiplo de 7 resultados con la misma probabilidad de $p^k(1-p)^{n-k}$ y coinciden con los uno contra el otro.
Debido a que el número total de resultados no es un múltiplo de 7, tenemos unos resultados que no se puede asignar a un número, y tienen una cierta probabilidad de que tenemos que descartar los resultados y empezar de nuevo.
El caso de uso 7 monedas de lanzamientos por turno
Intuitivamente, se podría decir que rodar los dados siete veces, sería muy interesante. Ya sólo tenemos que tirar 2 de la $2^7$ posibilidades. Es decir, el 7 veces los jefes y los tiempos 0 cabezas.
Para todos los otros $2^7-2$ posibilidades hay siempre un múltiplo de 7 casos con el mismo número de cabezas. Es decir, 7 casos con 1 cabezas, 21 casos con 2 cabezas, 35 casos con 3 cabezas, 35 casos con 4 cabezas, 21 casos con 5 cabezas, y 7 casos con 6 cabezas.
Así que si usted calcular el número (descartando 0 cabezas y 7 cabezas) $$X = \sum_{k=1}^{7} (k-1) \cdot C_k $$
with $C_k$ Bernoulli distributed variables (value 0 or 1), then X modulo 7 is a uniform variable with seven possible results.
Comparing different number of coin flips per turn
The question remains what the optimal number of rolls per turn would be. Rolling more dices per turn cost you more, but you reduce the probability to have to roll again.
The image below shows a manual computations for the first few numbers of coin flips per turn. (possibly there might be an analytical solution, but I believe it is safe to say that a system with 7 coin flips provides the best method regarding the expectation value for the necessary number of coin flips)
# plot an empty canvas
plot(-100,-100,
xlab="flips per turn",
ylab="E(total flips)",
ylim=c(7,400),xlim=c(0,20),log="y")
title("expectation value for total number of coin flips
(number of turns times flips per turn)")
# loop 1
# different values p from fair to very unfair
# since this is symmetric only from 0 to 0.5 is necessary
# loop 2
# different values for number of flips per turn
# we can only use a multiple of 7 to assign
# so the modulus will have to be discarded
# from this we can calculate the probability that the turn succeeds
# the expected number of flips is
# the flips per turn
# divided by
# the probability for the turn to succeed
for (p in c(0.5,0.2,0.1,0.05)) {
Ecoins <- rep(0,16)
for (dr in (5:20)){
Pdiscards = 0
for (i in c(0:dr)) {
Pdiscards = Pdiscards + p^(i)*(1-p)^(dr-i) * (choose(dr,i) %% 7)
}
Ecoins[dr-4] = dr/(1-Pdiscards)
}
lines(5:20, Ecoins)
points(5:20, Ecoins, pch=21, col="black", bg="white", cex=0.5)
text(5, Ecoins[1], paste0("p = ",p), pos=2)
}
Using an early stopping rule
note: the calculations below, for the expectation value of number of flips, are for a fair coin $p=0.5$, it would become a mess to do this for different $p$, but the principle remains the same (although different book-keeping of the cases is needed)
We should be able to choose the cases (instead of the formula for $$ X) de tal modo que no podría ser capaz de detener el anterior.
Con 5 coin flips tenemos para las seis posibles diferentes desordenada conjuntos de cabezas y colas:
1+5+10+10+5+1 conjuntos ordenados
Y podemos usar los grupos con diez casos (que es el grupo con 2 cabezas o el grupo con 2 colas) para elegir (con igual probabilidad) de un número. Esto ocurre en 14 de los 2^5=32 casos. Esto nos deja con:
1+5+3+3+5+1 conjuntos ordenados
Con un extra (6-t) tirón de la moneda tenemos para los siete posibles diferentes desordenada conjuntos de cabezas y colas:
1+6+8+6+8+6+1 conjuntos ordenados
Y podemos usar los grupos con ocho casos (que es el grupo con 3 cabezas o el grupo con 3 colas) para elegir (con igual probabilidad) de un número. Esto ocurre en 14 de 2*(2^5-14)=36 casos. Esto nos deja con:
1+6+1+6+1+6+1 conjuntos ordenados
Con otro (7-th) extra tirón de la moneda tenemos a los ocho posibles diferentes desordenada conjuntos de cabezas y colas:
1+7+7+7+7+7+7+1 conjuntos ordenados
Y podemos usar los grupos con siete casos (todos excepto el de todas las colas y todos los jefes de los casos) a elegir (con igual probabilidad) de un número. Esto ocurre en 42 de los 44 casos. Esto nos deja con:
1+0+0+0+0+0+0+1 conjuntos ordenados
(podemos continuar con esto, pero solo en la 49 ª paso hace esto nos da una ventaja)
Así que la probabilidad de seleccionar un número
- a las 5 de la volteretas en el es $\frac{14}{32} = \frac{7}{16}$
- a las 6 de la volteretas en el es $\frac{9}{16}\frac{14}{36} = \frac{7}{32}$
- a las 7 de la volteretas en el es $\frac{11}{32}\frac{42}{44} = \frac{231}{704}$
- no en 7 volteretas en el es $1-\frac{7}{16}-\frac{7}{32}-\frac{231}{704} = \frac{2}{2^7}$
Esto hace que la expectativa de valor para el número de lanzamientos en un turno, con la condición de que no hay éxito y p=0.5:
$$5 \cdot \frac{7}{16}+ 6 \cdot \frac{7}{32} + 7 \cdot \frac{231}{704} = 5.796875 $$
The expectation value for the total number of flips (until there is a success), conditional that p=0.5, becomes:
$$\left(5 \cdot \frac{7}{16}+ 6 \cdot \frac{7}{32} + 7 \cdot \frac{231}{704}\right) \frac{2^7}{2^7-2} = \frac{53}{9} = 5.88889 $$
The answer by NcAdams uses a variation of this stopping-rule strategy (each time come up with two new coin flips) but is not optimally selecting out all the flips.
The answer by Clid might be similar as well although there might be an uneven selection rule that each two coin flips a number might be chosen but not necessarily with equal probability (a discrepancy which is being repaired during later coin flips)
Comparison with other methods
Other methods using a similar principle are the one by NcAdams and AdamO.
The principle is: A decision for a number between 1 and 7 is made after a certain number of heads and tails. After an $x$ number of flips, for each decision that leads to a number $i$ there is a similar, equally probable, decision that leads to a number $j$ (the same number of heads and tails but just in a different order). Some series of heads and tails can lead to a decision to start over.
For such type of methods the one placed here is the most efficient because it makes the decisions as early as possible (as soon as there is a possibility for 7 equal probability sequences of heads and tails, after the $x$-th flip, we can use them to make a decision on a number and we do not need to flip further if we encounter one of those cases).
This is demonstrated by the image and simulation below:
#### mathematical part #####
set.seed(1)
#plotting this method
p <- seq(0.001,0.999,0.001)
tot <- (5*7*(p^2*(1-p)^3+p^3*(1-p)^2)+
6*7*(p^2*(1-p)^4+p^4*(1-p)^2)+
7*7*(p^1*(1-p)^6+p^2*(1-p)^5+p^3*(1-p)^4+p^4*(1-p)^3+p^5*(1-p)^2+p^6*(1-p)^1)+
7*1*(0+p^7+(1-p)^7) )/
(1-p^7-(1-p)^7)
plot(p,tot,type="l",log="y",
xlab="p",
ylab="expactation value number of flips"
)
#plotting method by AdamO
tot <- (7*(p^20-20*p^19+189*p^18-1121*p^17+4674*p^16-14536*p^15+34900*p^14-66014*p^13+99426*p^12-119573*p^11+114257*p^10-85514*p^9+48750*p^8-20100*p^7+5400*p^6-720*p^5)+6*
(-7*p^21+140*p^20-1323*p^19+7847*p^18-32718*p^17+101752*p^16-244307*p^15+462196*p^14-696612*p^13+839468*p^12-806260*p^11+610617*p^10-357343*p^9+156100*p^8-47950*p^7+9240*p^6-840*p^5)+5*
(21*p^22-420*p^21+3969*p^20-23541*p^19+98154*p^18-305277*p^17+733257*p^16-1389066*p^15+2100987*p^14-2552529*p^13+2493624*p^12-1952475*p^11+1215900*p^10-594216*p^9+222600*p^8-61068*p^7+11088*p^6-1008*p^5)+4*(-
35*p^23+700*p^22-6615*p^21+39235*p^20-163625*p^19+509425*p^18-1227345*p^17+2341955*p^16-3595725*p^15+4493195*p^14-4609675*p^13+3907820*p^12-2745610*p^11+1592640*p^10-750855*p^9+278250*p^8-76335*p^7+13860*p^6-
1260*p^5)+3*(35*p^24-700*p^23+6615*p^22-39270*p^21+164325*p^20-515935*p^19+1264725*p^18-2490320*p^17+4027555*p^16-5447470*p^15+6245645*p^14-6113275*p^13+5102720*p^12-3597370*p^11+2105880*p^10-999180*p^9+371000
*p^8-101780*p^7+18480*p^6-1680*p^5)+2*(-21*p^25+420*p^24-3990*p^23+24024*p^22-103362*p^21+340221*p^20-896679*p^19+1954827*p^18-3604755*p^17+5695179*p^16-7742301*p^15+9038379*p^14-9009357*p^13+7608720*p^12-
5390385*p^11+3158820*p^10-1498770*p^9+556500*p^8-152670*p^7+27720*p^6-2520*p^5))/(7*p^27-147*p^26+1505*p^25-10073*p^24+49777*p^23-193781*p^22+616532*p^21-1636082*p^20+3660762*p^19-6946380*p^18+11213888*p^17-
15426950*p^16+18087244*p^15-18037012*p^14+15224160*p^13-10781610*p^12+6317640*p^11-2997540*p^10+1113000*p^9-305340*p^8+55440*p^7-5040*p^6)
lines(p,tot,col=2,lty=2)
#plotting method by NcAdam
lines(p,3*8/7/(p*(1-p)),col=3,lty=2)
legend(0.2,500,
c("this method calculation","AdamO","NcAdams","this method simulation"),
lty=c(1,2,2,0),pch=c(NA,NA,NA,1),col=c(1,2,3,1))
##### simulation part ######
#creating decision table
mat<-matrix(as.numeric(intToBits(c(0:(2^5-1)))),2^5,byrow=1)[,c(1:12)]
colnames(mat) <- c("b1","b2","b3","b4","b5","b6","b7","sum5","sum6","sum7","decision","exit")
# first 5 rolls
mat[,8] <- sapply(c(1:2^5), FUN = function(x) {sum(mat[x,1:5])})
mat[which((mat[,8]==2)&(mat[,11]==0))[1:7],12] = rep(5,7) # we can stop for 7 cases with 2 heads
mat[which((mat[,8]==2)&(mat[,11]==0))[1:7],11] = c(1:7)
mat[which((mat[,8]==3)&(mat[,11]==0))[1:7],12] = rep(5,7) # we can stop for 7 cases with 3 heads
mat[which((mat[,8]==3)&(mat[,11]==0))[1:7],11] = c(1:7)
# extra 6th roll
mat <- rbind(mat,mat)
mat[c(33:64),6] <- rep(1,32)
mat[,9] <- sapply(c(1:2^6), FUN = function(x) {sum(mat[x,1:6])})
mat[which((mat[,9]==2)&(mat[,11]==0))[1:7],12] = rep(6,7) # we can stop for 7 cases with 2 heads
mat[which((mat[,9]==2)&(mat[,11]==0))[1:7],11] = c(1:7)
mat[which((mat[,9]==4)&(mat[,11]==0))[1:7],12] = rep(6,7) # we can stop for 7 cases with 4 heads
mat[which((mat[,9]==4)&(mat[,11]==0))[1:7],11] = c(1:7)
# extra 7th roll
mat <- rbind(mat,mat)
mat[c(65:128),7] <- rep(1,64)
mat[,10] <- sapply(c(1:2^7), FUN = function(x) {sum(mat[x,1:7])})
for (i in 1:6) {
mat[which((mat[,10]==i)&(mat[,11]==0))[1:7],12] = rep(7,7) # we can stop for 7 cases with i heads
mat[which((mat[,10]==i)&(mat[,11]==0))[1:7],11] = c(1:7)
}
mat[1,12] = 7 # when we did not have succes we still need to count the 7 coin tosses
mat[2^7,12] = 7
draws = rep(0,100)
num = rep(0,100)
# plotting simulation
for (p in seq(0.05,0.95,0.05)) {
n <- rep(0,1000)
for (i in 1:1000) {
coinflips <- rbinom(7,1,p) # draw seven numbers
I <- mat[,1:7]-matrix(rep(coinflips,2^7),2^7,byrow=1) == rep(0,7) # compare with the table
Imatch = I[,1]*I[,2]*I[,3]*I[,4]*I[,5]*I[,6]*I[,7] # compare with the table
draws[i] <- mat[which(Imatch==1),11] # result which number
num[i] <- mat[which(Imatch==1),12] # result how long it took
}
Nturn <- mean(num) #how many flips we made
Sturn <- (1000-sum(draws==0))/1000 #how many numbers we got (relatively)
points(p,Nturn/Sturn)
}
another image which is scaled by $p*(1-p)$ para una mejor comparación: