Thursday, May 14, 2015

Python: random (1)


Abstract: get random numbers and random samples. Random testing is also available.

Frequency of randomly selected numbers or samples should be close to the theoretical number, and the mean error should be less than 10%.



The result:

Random integer: [19, 92, 76, 54, 51, 30, 78, 57, 16, 64]

36

Random sampling: ['2', 'f', 'b', 'g', '0', 'l', '7', 'w', 'n', '1']



Frequency of numbers by permutation: [88, 89, 90, 90, 91, 94, 96, 97, 98, 98, 99, 99, 100, 101, 101, 101, 102, 102, 102, 102, 102, 102, 102, 103, 103, 103, 103, 104, 104, 104, 104, 104, 105, 105, 105, 106, 106, 108, 108, 108, 108, 109, 109, 109, 109, 110, 110, 110, 110, 111, 111, 112, 112, 112, 112, 113, 113, 114, 114, 114, 114, 114, 115, 116, 116, 116, 117, 117, 118, 119, 119, 119, 119, 120, 120, 120, 121, 121, 121, 121, 121, 122, 123, 123, 124, 125, 125, 125, 131, 133, 134]

Error percentage is [12, 11, 10, 10, 9, 6, 4, 3, 2, 2, 1, 1, 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 6, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 14, 14, 14, 14, 14, 15, 16, 16, 16, 17, 17, 18, 19, 19, 19, 19, 20, 20, 20, 21, 21, 21, 21, 21, 22, 23, 23, 24, 25, 25, 25, 31, 33, 34] and mean value 11



Frequency of numbers by permutation: [247, 253, 254, 259, 260, 260, 262, 264, 265, 267, 268, 269, 273, 274, 274, 276, 276, 277, 278, 280, 281, 281, 283, 283, 285, 287, 288, 289, 289, 289, 292, 293, 296, 298, 302, 328]

Error percentage is [31, 25, 24, 19, 18, 18, 16, 14, 13, 11, 10, 9, 5, 4, 4, 2, 2, 1, 0, 2, 3, 3, 5, 5, 7, 9, 10, 11, 11, 11, 14, 15, 18, 20, 24, 50] and mean value 12


The script:

# -*- coding: utf-8 -*-

"""

Created on Thu May 14 12:32:27 2015



@author: yuan

"""



import random



class random_operations:

   

    def __init__(self, number):

        self.number=number

       

    def random_int(self, start_end):

        arr=[]

        for i in range(self.number):

            value=random.randint(start_end[0],start_end[1])

            arr.append(value)

        return arr

   

    def random_sampling(self, pool):

        arr=[]

        private_pool=list(pool)

        for i in range(self.number):

            value=random.choice(private_pool)

            private_pool.remove(value)

            arr.append(value)

        return arr             

       

    def random_testing(self, permutation, FUN, *args):

        counting={}

        for i in range(permutation):

            random_list=[]

            random_list=FUN(args[0])

            #print random_list

            for a in random_list:

                if counting.has_key(a):

                    counting[a] +=1

                else:

                    counting[a]=1

        return counting

               

       

#main program

rn=random_operations(number=10)



#get 10 random integer numbers between 10-100

start_end=(10,100)

print 'Random integer:', rn.random_int(start_end)



#get 10 randomly sampling from array

pool=list('1234567890qwertyuiopasdfghjklzxcvbnm')

print len(pool)

print 'Random sampling:', rn.random_sampling(pool)



#permutate random

#perfect number should be close to 100

counting=rn.random_testing(1000, rn.random_int, start_end )

fre=sorted(counting.values())

print '\nFrequency of numbers by permutation:', fre

err=map(lambda x: abs((x-100)*100/100), fre)

print 'Error percentage is %s and mean value %s' % (err, sum(err)/len(err))



#perfect number should be close to 278

counting=rn.random_testing(1000, rn.random_sampling, pool )

fre=sorted(counting.values())class random_operations:

   

    def __init__(self, number):

        self.number=number

       

    def random_int(sel

print '\nFrequency of numbers by permutation:', fre

err=map(lambda x: abs((x-278)*100/100), fre)

print 'Error percentage is %s and mean value %s' % (err, sum(err)/len(err))


No comments:

Post a Comment