How to implement Brownian motion with the thread of Python development

Source: Internet
Author: User
In this paper, we describe the method of developing the thread of Python to implement Brownian motion. Share to everyone for your reference, as follows:

Here I will give you an example of the thread in Python to implement Brownian motion.

Here's how it works:

Code section:

# Brownian Motion--an example of a multi-threaded Tkinter program.from Tkinter import *import randomimport threadingimpo RT timeimport sys# Canvas Size width = 400HEIGHT = 300SIGMA = 10BUZZ = 2RADIUS = 2LAMBDA = 10FILL = ' Red ' stop = 0 # Set when main l OOP exitsdef particle (Canvas):  r = RADIUS  x = Random.gauss (width/2.0, SIGMA)  y = Random.gauss (height/2.0, SIGMA)  p = canvas.create_oval (X-r, Y-r, X+r, Y+r, Fill=fill) while not  stop:    dx = Random.gauss (0, BUZZ)    D y = Random.gauss (0, BUZZ)    dt = random.expovariate (LAMBDA)    try:      canvas.move (P, dx, dy)    except Tclerror :      break    time.sleep (DT) def main ():  global stop  root = Tk ()  canvas = canvas (root, Width=width, Height=height)  canvas.pack (fill= ' both ', expand=1)  #粒子数目  np =  if sys.argv[1:]:    np = Int ( SYS.ARGV[1]) for  I in range (NP):    t = Threading. Thread (target=particle, args= (canvas))    T.start ()  try:    root.mainloop ()  finally:    stop = 1main ()

I hope this article is helpful for Python program design.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.