• Python
  • 54 lines
  • 10 views
  • Pasted by anonymous on January 29, 2013
#!/usr/bin/env python

from __future__ import division
import random, re, math, sys

DEBUG = False

if len(sys.argv) != 2:
    print "Please pass exactly one arg: the num of random"
    print "nums to have in our run."
    sys.exit()

num_nums = int(sys.argv[1])

sample = 'abcdefghijkl' + '0123456789'
random_strings = []

reg = re.compile(r'[1-9]\d{2,}')

for i in range(num_nums):
    temp_str_ar = []
    for j in range(10):
        temp_str_ar.append(random.choice(sample))
    random_strings.append( ''.join(temp_str_ar) )

nums = []
num_chars = dict.fromkeys(range(3, 11), 0)

for s in random_strings:
    mat = reg.search(s)
    if mat:
        ss = mat.group()
        num_chars[len(ss)] += 1
        nums.append(int(ss))

if DEBUG:
    print "DEBUG, random strings:", random_strings
    print "DEBUG, nums:          ", nums
    print "DEBUG, num_chars:     ", num_chars

print "Generated %s random strings." % len(random_strings)
print "Found %s numbers." % len(nums)
print "Min:", min(nums)
print "Max:", max(nums)
print "Distribution:"
for num, num_times in num_chars.items():
    print "  ", num, num_times

logs = [math.log(x) for x in nums]
if DEBUG:
    print "DEBUG, logs: "
avg = sum(logs) / len(logs)
print "Avg of natural logs of nums: %.2f" % avg
print '-' * 42

Did you know? CLOSE

  • There are keyboard shortcuts!
    • When Creating A Paste
      • ALT+P Toggle Private
      • CTRL+Enter Create Paste
      • ALT+W Toggle word wrap
    • When Viewing A Paste
      • ALT+G Go to a line
      • ALT+CTRL+E Edit the paste
      • ALT+R Show the raw code
  • There are URL options!
    • When Creating A Paste
      • ?lang=Javascript to default to javascript
    • When Viewing A Paste
      • #L-N Jump to line number N
?