[Tutor] Random equation generator

Rich Lovely roadierich at googlemail.com
Fri Dec 5 14:30:03 CET 2008


When you say linear, I'm assuming fitting y=mx+c, and passing through  
points?

The line through points (x1, y1) and (x2,y2) is

y - y1 = (y2-y1) / (x2-x1) * (x-x1)

That multiplies out to:

y = (y2-y1)/(x2-x1) * x - (y2-y1)/(x2-x1) + y1
That gives m = (y2-y1)/(x2-x1) and c =  y1 - (y2-y1)/(x2-x1)

you can then create a class to represent the line:

class Line(object):
     def __init__(self, (x1,y1), (x2,y2)):
         "create a line passing through points (x1,y1) and (x2,y2)  
(inputted as tuples)"
         self.m = (y2-y1)/(x2-x1)
         self.c = y1 - self.m

     def is_on_line(self,(x,y)):
         'Test if point represtented by an (x,y) tuple is on the line"
         if self. m * x + self.c == y:
             return True
         else:
             return False

     def __str__(self):
         "returns the equation of the line in the form y=mx+c.  Might  
be quite long if floats are involved."
         print "y=%dx + %d" % (self.m, self.c)


Then all you have to do is choose a pair of points, then iterate over  
the list, testing each point in turn, wash, rinse and repeat.

Does that help?

On 4 Dec 2008, at 20:28, TexasJerky100 at aol.com wrote:

>    I am starting out with 7 fixed reference points.  From there I  
> want  a program that can randomly
> generate linear equations.  After the equations are generated I  
> would then like to randomly insert
> the 7 fixed reference points into the equations and calculate the  
> results.  I currently have several
> programs that can generate random string of words from a file that  
> contains a list of word but is not
> much help creating random equations.
>   Do you know if there is such a program that can do what  I am  
> trying to get accomplished??
>
> Thanks
>
> Frank Hopkins
> Houston, Tx
>
>
>
> Make your life easier with all your friends, email, and favorite  
> sites in one place. Try it now.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081205/913318cc/attachment.htm>


More information about the Tutor mailing list