miércoles, 29 de agosto de 2012

Statistical test homework

Statistical test

Hi, in this homework i made a statistical test to my keys generated in the past homework, this is the link to my past homework (http://triana-integrador-cripto.blogspot.mx/2012/08/one-time-pad.html).

Ok, this is an example of 10 keys generated in mi program.


A numeric sequence is said to be statistically random when it contains no recognizable patterns or regularities.

Any method to generate random numbers must satisfy next conditions:

- Uniformly distributed
- Without constant repetition in a specified length (often not repeatable)
- Numbers must be independent of each other
- The series should be reproducible
- Numbers must be generated quickly
Well, there are many ways or methods to determine if a sequence of number are random.
I will use the method called: Media Test (Prueba de medias)


Media Test

Ok, media test method consists in verify that generated numbers have a statics media between upper and lower limits.


The 3 important elements to work with media test are the next:


* R = Is the summation of all numbers in the test, multiplied by (1/ total of numbers)
This number will be compared whit  the lower and upper limits. If the R value is in the range of the limits, the numbers are random, if R is out of range with the limits, the numbers are not random.
R is calculated:

 

  

* Lower limit = Is the lowest limit to compare with the "r"
Lower limit is calculated:


Za = Z value of alfa (not acceptance level). You must use the normal distribution table
* Upper limit = Is the highest limit to compare with the "r"
Upper limit is calculated:



We are going to set an alfa, alfa value is the number of error probabilities. Assuming we want a 95 percent of acceptance level, alfa will be 5 percent of not acceptance, so alfa = 0.05.

I'll create the keys like in the one time pad homework, like this:




Then i created a code to count all 0 and 1 in the text file, and the total of numbers.
zAlfa = 0.5199, because alfa = 0.05, here is the table were i got "z" value: http://www.tuveras.com/estadistica/normal/tabla.htm






And finally with all the number added, i calculated the value of "r". And too calculated the values of lower and upper limits.

Like this:

This is the entire code (if you want to copy it):

#!/usr/bin/python
#Carlos Triana
import random
import math
from numpy import *
def createKeys():
file1 = open('keys1.txt', 'w')#Creating file1
i = 0
j = 0
while i<10:
while j<7:
number = random.randint(0,2)
file1.write(str(number))#Filling file texts with random numbers
j = j +1
file1.write('\n') #To separate keys
j = 0
i = i + 1
file1.close()
mediaTest()
def mediaTest():
file1 = open('keys1.txt', 'r')#Reading key
x = 0
r = 0.0
zero = 0.0 #To count the total of zero
one = 1.0 #To count the total of one
upperLimit = 0.0 #Upper limit value
lowerLimit = 0.0 #Lower limit value
zAlfa = 0.5199 #Value of Z whit alfa = 0.05
for i in file1.xreadlines(): #To read each line of numbers
size = len(i)
while x < size:
if i[x] == "0":
zero = zero + 1.0 #If number = 0, increase counter(zero)
elif i[x] == "1": #If number = 1, increase counter(one)
one = one + 1.0
x = x + 1
x = 0
file1.close()
one = one - 1.0
total = 0.0
total = zero + one #Total of numbers
r = (1/total)*(one) #'r' is the number to compare whit limits(upper,lower)
lowerLimit = (0.5) - ((zAlfa/2)*(1/math.sqrt(12*(total)))) #Calculating lowerLimit
upperLimit = (0.5) + ((zAlfa/2)*(1/math.sqrt(12*(total)))) #Calculating upperLimit
if (r >= lowerLimit and r <= upperLimit):
print "\nGenerated numbers are random :)\n"
#If 'r' is between lowerLimit and upperLimim, numbers are random
else:
print "\nGenerated numbers are not random at all :(\n"
#If 'r' is out of the limits, numbers are not random
print "zero = " + str(zero) #Printing total of 0 in the txt
print "one = " + str(one) #Printing total of 1 in the txt
print "total = " + str(total) #Printing total of numbers
print "r = " + str(r) #Printing R
print "lowerLimit = " + str(lowerLimit) #Printing Lower and upper limits
print "upperLimit = " + str(upperLimit)
print
def main():
createKeys()
main()
view raw MediaTest.py hosted with ❤ by GitHub

These are some screen shots of my program running: 

TEST:

Generated keys                                                                              Results



Bibliography:


If you have any doubt you can leave it in comments. 
Sorry for my bad english. Thank you! 


1 comentario:

  1. The example keys seem horribly short. It would have been good to write a paragraph interpreting the results. 6 pts.

    ResponderEliminar