random numpy error?

3,714 views
Skip to first unread message

Søren Kyale

unread,
May 3, 2013, 6:08:46 PM5/3/13
to uic-mcs...@googlegroups.com
I get the following response when running my tests.

When the error doen't pop up, I get an everything passed response.

Is this a numpy mac bug?

Traceback (most recent call last):
  File "sort.py", line 172, in <module>
    if __name__ == "__main__": test()
  File "sort.py", line 121, in test
    sortMe = list(numpy.random.randint(numpy.random.randint(1000), size=numpy.random.randint(108)))
  File "mtrand.pyx", line 854, in mtrand.RandomState.randint (numpy/random/mtrand/mtrand.c:5904)
ValueError: low >= high

test code:
check time:  27 2.40802764893e-05
bubble time:  25 0.000274181365967
check time:  25 2.19345092773e-05
bubblem time:  87 0.00342512130737
check time:  87 5.10215759277e-05
insertion time:  59 0.000833988189697
check time:  59 3.79085540771e-05
lame time:  33 0.00018310546875
check time:  33 2.71797180176e-05
All tests have passed

Press ENTER or type command to continue

# test:none -> none:
# here be tests:
def test():
   import numpy, time
   isPassing = True
   
   for i in range (100):

      sortMe = list(numpy.random.randint(numpy.random.randint(1000), size=numpy.random.randint(108)))
      jackHarkness = time.time()
      bubble(sortMe)
      print "bubble time: ", len(sortMe), time.time() - jackHarkness
      jackHarkness = time.time()
      passed = isSorted(sortMe) 
      print "check time: ", len(sortMe), time.time() - jackHarkness
      isPassing *= passed
      if not passed: print "bubble failed"
      
      sortMe = list(numpy.random.randint(numpy.random.randint(1000), size=numpy.random.randint(108)))
      jackHarkness = time.time()
      bubblem(sortMe)
      print "bubblem time: ", len(sortMe), time.time() - jackHarkness
      jackHarkness = time.time()
      passed = isSorted(sortMe) 
      print "check time: ", len(sortMe), time.time() - jackHarkness
      isPassing *= passed
      if not passed: print "bubblem failed"
      
      sortMe = list(numpy.random.randint(numpy.random.randint(1000), size=numpy.random.randint(108)))
      jackHarkness = time.time()
      insertion(sortMe)
      print "insertion time: ", len(sortMe), time.time() - jackHarkness
      jackHarkness = time.time()
      passed = isSorted(sortMe) 
      print "check time: ", len(sortMe), time.time() - jackHarkness
      isPassing *= passed
      if not passed: print "insertion failed"


      sortMe = list(numpy.random.randint(numpy.random.randint(1000), size=numpy.random.randint(108)))
      jackHarkness = time.time()
      lame(sortMe)
      print "lame time: ", len(sortMe), time.time() - jackHarkness
      jackHarkness = time.time()
      passed = isSorted(sortMe) 
      print "check time: ", len(sortMe), time.time() - jackHarkness
      isPassing *= passed
      if not passed: print "lame failed"

   sortMe = list(numpy.random.randint(numpy.random.randint(1000), size=numpy.random.randint(108)))
   sortMeTwo, sortMeThree = list(sortMe), list(sortMe)
   bubble(sortMe), bubblem(sortMeTwo), insertion(sortMeThree)
   passed = sortMe == sortMeTwo and sortMeThree == sortMeTwo
   isPassing *= passed
   if not passed: print "comparison failed"
   
   if isPassing:
      print "All tests have passed"

if __name__ == "__main__": test()

Jeremy Kun

unread,
May 3, 2013, 7:11:29 PM5/3/13
to Søren Kyale, uic-mcs...@googlegroups.com
No this is a logic error. The random function accepts a low and a high, and generates a number low <= x < high. Since you're giving random numbers as inputs (that is, low is a random number and high is a random number), every now and then you'll be calling the function with high smaller than low, upon which numpy raises an error. Indeed, if I asked you to generate a random number x such that 9 < x < 1, what would you do?

Jeremy Kun
Mathematics Graduate Student
University of Illinois at Chicago


--
You received this message because you are subscribed to the Google Groups "uic-mcs260-s13" group.
To unsubscribe from this group and stop receiving emails from it, send an email to uic-mcs260-s1...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Søren Kyale

unread,
May 3, 2013, 7:34:47 PM5/3/13
to uic-mcs...@googlegroups.com, Søren Kyale
Yes. That would be difficult. 

It was having trouble with 0 < x < 0

adding one fixed the problem.

      sortMe = list(numpy.random.randint(numpy.random.randint(1000)+1, size=numpy.random.randint(108)))

Thank you.
Reply all
Reply to author
Forward
0 new messages