Well, the next step is to verify the keys of the One-Time-Pad randomly generated by Python's random method
There are many types of methods for checking if the numbers are random.
Characteristics:
- Uniformly distributed
- No numbers repeated on a specified length
- Numbers must be independent of each other
- The series should be reproducible
- The numbers must be generated quickly
Part of the previous program had to generate the random numbers
Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def crearlibretas(): ## aqui creamos las libretas para la encriptacion | |
x = 0 | |
y = 0 | |
pepe = open("libreta1.txt","w") ##libreta el que envia | |
ana = open("libreria2.txt","w") | |
while y<=20: | |
while x<= 20: | |
numeror = random.randint(0,2) ##valor random entre: 0<= x <2 | |
#float(numeror) | |
pepe.write(str(numeror)) ## escribimos el caracter | |
ana.write(str(numeror)) | |
# maria.write(str(numeror)) ## escribimos el mismo caracter | |
x = x + 1 | |
#print "y = ",y,"" | |
pepe.write("\n") ##salto de linea en el archivo | |
ana.write("\n") |
Test used "Run Test"
In this Test is verified if the hypothesis of a number is really random between two elements, in this case 0 and 1.
Parameters
amount of the first element in this case "0"
amount of the second element in this case "1"
amount of elements
amount of blocks
Regarding the blocks, a block is when the same element is repeated until meet the other element and it starts over with a new block:
For example:
In this case, we have 5 Blocks
Having this elements clearly defined, we can begin seeing how the test works
First, we get the arithmetic mean:
After we got the arithmetic mean, the variance:
Finally we got the probability of Z
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
entrada = open("libreta1.txt") | |
nn = 0 | |
nm = 0 | |
y = 0 | |
totalb = 0 | |
bloquea = 0 | |
#bloqueb = 0 | |
x = 0 | |
#def bloques(): | |
u = 0 | |
##leemos la libreta que contienelos numeros | |
for i in entrada.xreadlines(): | |
tam = len(i) | |
######guardamos la cantidad de 1 y 0###### | |
while x < tam: | |
if i[x] == "0": | |
nm = nm + 1 | |
else: | |
nn = nn + 1 | |
#x = x + 1 | |
###chekamos cauntos bloques tiene########### | |
if y != 0: | |
if i[x] == bloquea: | |
print "si es del bloque" | |
else: | |
totalb = totalb + 1 | |
bloquea = i[x] | |
print "otro numero diferente" | |
else: | |
totalb = totalb + 1 | |
bloquea = i[x] | |
y = y + 1 | |
x = x + 1 | |
x = 0 | |
#u = float((2(nn*nm)/(nn + nm)) + 1) | |
##proceso a realizar########################## | |
#####primero sacar la media aritmetica####### | |
u = (2*nn*nm)/(nn + nm) + 1 | |
#desviacion estandar recordando que esta al cuadrado ########## | |
desviacion = ((u - 1)*(u - 2))/(nn + nm - 1) | |
#####sacamos la raiz############################# | |
des = math.sqrt(desviacion) | |
####sacamos la probabilidad o resultado para comprobarlo con la probabilidad de Z0.005 = 1.96## | |
z = (totalb - u)/des | |
print "total de bloques = ",totalb,"" | |
print "total de 1 = ",nn,"total de 0= ",nm,"" | |
print u | |
print desviacion | |
print z | |
####comparamos si s aleatorio o no 1.96 se saca en las tablas de distribucion | |
if z < 1.960: | |
print "Alta probabilidad que si sean random" | |
else: | |
print "No hay probabilidad" | |
entrada.close() |
In this case is randomly, but in mayor of the case arenot randomly
Is randomly = 20%
Not randomly = 80 %
Test was performed 10 times
Sorry, here is more information, the value 1.96 is found in the critical value, it depends of the level of reliability, in this case 95 percent.
Graphics and table:
Here is the table:
1 - a | a/2 | Za/2 |
0.90 | 0.05 | 1.645 |
0.95 | 0.025 | 1.96 |
0.99 | 0.005 | 2.575 |
Thanks, have a nice day :D
http://www.ub.edu/aplica_infor/spss/cap5-4.htm
http://nlab.ee.tokushima-u.ac.jp/nishio/Pub-Data/CONF/C174.pdf
http://en.wikipedia.org/wiki/Wald%E2%80%93Wolfowitz_runs_test
http://webspace.ship.edu/pgmarr/Geo441/Examples/Runs%20Test.pdf
http://www.ditutor.com/inferencia_estadistica/valores_criticos.html
http://nlab.ee.tokushima-u.ac.jp/nishio/Pub-Data/CONF/C174.pdf
http://en.wikipedia.org/wiki/Wald%E2%80%93Wolfowitz_runs_test
http://webspace.ship.edu/pgmarr/Geo441/Examples/Runs%20Test.pdf
http://www.ditutor.com/inferencia_estadistica/valores_criticos.html
It would have been nice to include more tests. 5 pts.
ResponderEliminar