Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #!/usr/bin/env python
  2.  
  3. import random
  4. #from math import log, exp, sqrt, pi
  5.  
  6. def test_seed_state():
  7.     """test seed() and getstate()/setstate()
  8.    """
  9.     # random ought to be able to deal with seeds in any form, of follows.
  10.     # following code shouldn't cause an exception.
  11.     random.seed()
  12.     random.seed(0)
  13.     random.seed(-1)
  14.     random.seed(0.1)
  15.     random.seed(-0.1)
  16.     random.seed("a")
  17.     random.seed("abc")
  18.     random.seed("abcd")
  19.     random.seed("fasdfasdfasdfadgaldhgldahlgahdlghadlgladh")
  20.     random.seed("lxhlh90yowhldshlgah;")
  21.    
  22.     # state1 and state2 should be different for different seeds
  23.     random.seed(1)
  24.     state1 = random.getstate()
  25.     random.seed(2)
  26.     state2 = random.getstate()
  27.     rep = 0
  28.     for ind in range(len(state1)):
  29.         elem1 = state1[ind]
  30.         elem2 = state2[ind]
  31.         if (elem1 == elem2): rep += 1
  32.     if (rep > len(state1) / 2):
  33.         print("rep = ", rep, "len(state1) = ", len(state1))
  34.         raise "state1 and state2 should be different"
  35.    
  36.     # for the same seeds, state1 and state2 should be the same
  37.     random.seed(100)
  38.     state1 = random.getstate()
  39.     random.seed(100)
  40.     state2 = random.getstate()
  41.     rep = 0
  42.     for ind in range(len(state1)):
  43.         elem1 = state1[ind]
  44.         elem2 = state2[ind]
  45.         if (elem1 == elem2): rep += 1
  46.     if (rep != len(state1)):
  47.         raise "state1 and state2 should be the same"
  48.  
  49. def test_jumpahead():
  50.     """jumpahead will change the pseudo-number generator's internal state
  51.    """
  52.     random.seed()
  53.     state1 = random.getstate()
  54.     random.jumpahead(20)
  55.     state2 = random.getstate()
  56.     rep = 0
  57.     for ind in range(len(state1)):
  58.         elem1 = state1[ind]
  59.         elem2 = state2[ind]
  60.         if (elem1 == elem2): rep += 1
  61.     if (rep > len(state1) / 2):
  62.         raise "state1 and state2 can't be the same"
  63.        
  64. def test_setstate():
  65.     """
  66.    """
  67.     random.seed()
  68.     oldState = random.getstate()
  69.     oldRandSeq = [random.random() for i in range(10)]
  70.     random.setstate(oldState)
  71.     newRandSeq = [random.random() for i in range(10)]
  72.     rep = 0
  73.     for ind in range(len(oldRandSeq)):
  74.         elem1 = oldRandSeq[ind]
  75.         elem2 = newRandSeq[ind]
  76.         if (elem1 == elem2): rep += 1
  77.     if (rep != len(oldRandSeq)):
  78.         raise "oldRandSeq and newRandSeq should be the same"
  79.  
  80. def test_random():
  81.     """generate a random number list
  82.    """
  83.     x = [random.random() for i in range(100)]
  84.    
  85. def test_distribution():
  86.     """these lines are borrowed from python, they shouldn't
  87.        cause any exception.
  88.    """
  89.     g = random
  90.     g.uniform(1,10)
  91.     g.paretovariate(1.0)
  92.     g.expovariate(1.0)
  93.     g.weibullvariate(1.0, 1.0)
  94.     g.normalvariate(0.0, 1.0)
  95.     g.lognormvariate(0.0, 1.0)
  96.     g.vonmisesvariate(0.0, 1.0)
  97.     g.gammavariate(0.01, 1.0)
  98.     g.gammavariate(1.0, 1.0)
  99.     g.gammavariate(200.0, 1.0)
  100.     g.betavariate(3.0, 3.0)
  101.  
  102. def test_randrange():
  103.     """these input to randrange() shouldn't cause any exception.
  104.    """
  105.     random.randrange(100000)
  106.     random.randrange(-100000)
  107.     random.randrange(0)
  108.     random.randrange(-10.2)
  109.    
  110.     random.randrange(-10, 10)
  111.     random.randrange(2, 1000)
  112.     random.randrange(0, 1)
  113.     random.randrange(-1, 0)
  114.    
  115.     random.randrange(10, 2000, 2)
  116.     random.randrange(-2000, 100, 5)
  117.     random.randrange(-1000.3, 1000.7, 2)
  118.  
  119. def test_randint():
  120.     """for any valid pair (a, b), randint(a, b) should lay between [a, b]
  121.    """
  122.     for i in range(1000):
  123.         r = random.randint(-10000, 10000)
  124.         if (-10000 <= r <= 10000): continue
  125.         else: raise "error: random.randint()"
  126.  
  127. def test_choice():
  128.     """random.choice() should be able to deal with string, list.
  129.    """
  130.     S = "abcdefg123*@#$%)("
  131.     L = [1, 2, 3, -1, 0.2, -0.1, -10000, "cyc"]
  132.    
  133.     if random.choice(S) not in S:
  134.         raise "error: random.choice(S)"
  135.    
  136.     if random.choice(L) not in L:
  137.         raise "error: random.choice(L)"
  138.  
  139. def test_shuffle():
  140.     """test random.shuffle() on list. since string is not writable in-place,
  141.        random.shuffle() can not be applied on string.
  142.        Note: to copy items from a list to a new list, must use syntax like:
  143.            newList = oldList[:]
  144.        if use syntax like: newList = oldList, newList is just an alias of oldList.
  145.    """
  146.     oldL = [1, 2, 3, -1, 0.2, -0.1, -10000, "cyc"]
  147.     newL = oldL[:]
  148.    
  149.     random.shuffle(newL)
  150.    
  151.     rep = 0
  152.     for ind in range(len(oldL)):
  153.         elem1 = oldL[ind]
  154.         elem2 = newL[ind]
  155.         if (elem1 == elem2): rep += 1
  156.     if (rep > len(oldL) / 2):
  157.         raise "oldL and newL shouldn't be the same"
  158.        
  159. def test_53_bits_per_float():
  160.     pass
  161.        
  162. def main():
  163.     test_seed_state()
  164.     test_jumpahead()
  165.     test_setstate()
  166.     test_random()
  167.     test_distribution()
  168.     test_randrange()
  169.     test_randint()
  170.     test_choice()
  171.     test_shuffle()
  172.     test_53_bits_per_float()
  173.     print("#OK")
  174.  
  175. if __name__ == '__main__':
  176.     main()
  177.