"""File: rand_data.py Purpose: Generate input for the histogram program Run: python rand_data.py Input: n: number of random values min_rv: minimum random value max_rv: maximum random value b: number of bars min_v: min value for smallest subinterval max_v: max value for largest subinterval filename:name of file in which to store output Output: n values b min_v max_v Note: The output is formatted so that it can be used as input to the histogram program. """ from random import random #---------------------------------------------------------------------- def gen_vals(vals, n, min_rv, max_rv): """Generate n pseudo-random floats between min_rv and max_rv Store the floats in vals""" for i in range(n): x = random() y = (max_rv-min_rv)*x + min_rv vals.append(y) #---------------------------------------------------------------------- def print_data(filename, n, vals, b, min_v, max_v): """Open filename for writing. Write remaining values to this file""" fout = open(filename, "w") fout.write(str(n) + '\n') for x in vals: fout.write(str(x) + '\n') fout.write(str(b) + '\n') fout.write(str(min_v) + '\n') fout.write(str(max_v) + '\n') fout.close #---------------------------------------------------------------------- # Main program n = int(raw_input("How many random values?\n ")) min_rv = float(raw_input("What's the smallest random value?\n ")) max_rv = float(raw_input("What's the largest random value?\n ")) vals = [] gen_vals(vals, n, min_rv, max_rv) b = int(raw_input("How many bars will there be?\n ")) min_v = float(raw_input("What's the smallest value in the histogram?\n ")) max_v = float(raw_input("What's the largest value in the histogram?\n ")) filename = raw_input("What's the name of the output file?\n ") print_data(filename, n, vals, b, min_v, max_v)