#!/usr/bin/python
# Simulate data with a linear dependence between x and y.  This
#creates an output file named "linear_sim.txt" which can be read as a
#comma separated value file.

# Get the default random number generator (the MT Twister), it's one
# of the best known (much better than the ROOT default).  By default,
# it will be seeded using the current time.
import random

def NewPoint(x):
    """Create a new random value on the line.

    A Gaussian distributed random value associated for X will be
    returned as tuple of the value and it's uncertainty. The slope,
    intercept and standard deviation of the distribution can be set by
    changing the variables
    "slope", "intercept" and "sigma" in the function body.
    """
    sigma = 1.0
    slope = 0.5
    intercept = 1.0
    mean = slope*x + intercept
    return (random.gauss(mean,sigma), sigma)

def MakePoints(name="linear_sim.txt", count=10):
    """Generate a new set of random points

    This is the main function for this script.  It will open a new
    file, "name", and the write "count" new points to the file."
    """
    output = file(name,"w")
    for trial in xrange(1,count+1):
        mean = 0
        sigma = 5
        x = random.gauss(mean,sigma)
        (y, dy) = NewPoint(x)
        output.write("%d, %0.3f, %0.3f, %0.3f\n" % (trial, x, y, dy))

# This is a simple python trick so that linear_sim can be included as
# a module, or used as a script.  The "__name__" variable always
# contains the name of the current module.  If a file is executed as a
# script, the module name will be "__main__", otherwise it will be the
# name of the file minus a ".py" extention.
if __name__ == '__main__': MakePoints()
    
