python turtle,random,math

# List imports here:

import turtle

import random

import math

# List constants here (NO MAGIC NUMBERS!):

NUMBER_DARTS = 1000

LEFT_BOUND = -2

RIGHT_BOUND = 2

TOP_BOUND = -2

BOTTOM_BOUND = 2

PEN_SIZE = 3

SQUARE_WIDTH = 2

SQUARE_TOP = 1

SQUARE_LEFT = -1

DOT_SIZE = 5

# Draws square given turtle, width and top left XY position

# param grapher (Turtle)

# param width (int)

# param topLeftX (int)

# param topLeftY (int)

def drawSquare( grapher, width, topLeftX, topLeftY ):

grapher.penup()

grapher.setpos(topLeftX, topLeftY)

grapher.setheading(0)

grapher.pendown()

grapher.forward(width)

grapher.right(90)

grapher.forward(width)

grapher.right(90)

grapher.forward(width)

grapher.right(90)

grapher.forward(width)

grapher.penup()

# Draws line given turtle and endpoints

# param grapher (Turtle)

# param xStart (int)

# param yStart (int)

# param xEnd (int)

# param yEnd (int)

def drawLine( grapher, xStart, yStart, xEnd, yEnd ):

grapher.penup()

grapher.setpos(xStart, yStart)

grapher.setheading(0)

angle_rad = math.atan2(yEnd - yStart, xEnd - xStart)

angle = angle_rad * 180.0 / math.pi

dist = math.sqrt((yEnd - yStart) * (yEnd - yStart) + (xEnd - xStart) * (xEnd - xStart))

grapher.pendown()

grapher.left(angle)

grapher.forward(dist)

grapher.penup()

# Draws a dot with given x, y coordinates

# param grapher (Turtle)

# param x (double)

# param y (double)

# param color (string)

def drawDot( grapher, x, y, color ):

#grapher.pencolor(color)

grapher.penup()

grapher.setpos(x, y)

grapher.dot(DOT_SIZE, color)

grapher.penup()

# Sets up 4X4 area with x- and y-axis to simulate dartboard

# param board - window that will simulate board

# param grapher - turtle that will do drawing

def setUpDartboard( board, grapher ):

# set up 4X4 area and set pensize

board.setworldcoordinates(LEFT_BOUND, TOP_BOUND, RIGHT_BOUND, BOTTOM_BOUND)

grapher.pensize(PEN_SIZE)

# Draw board

drawSquare( grapher, SQUARE_WIDTH, SQUARE_LEFT, SQUARE_TOP )

# Draw x- and y-axes

drawLine( grapher, LEFT_BOUND, 0, RIGHT_BOUND, 0 )

drawLine( grapher, 0, TOP_BOUND, 0, BOTTOM_BOUND )

# (Predicate Function)

# Determines whether or not dart falls inside unit circle with center at 0,0

# param dart (Turtle)

# return True if in circle, False if not

def inCircle( x, y ):

if x*x + y*y <= 1:

return True

else:

return False

# Algorithm for Monte Carlo simulation

# param numberDarts (int)

# param grapher (Turtle)

# return approximation of pi (float)

def montePi( numberDarts, grapher ):

# Initialize inCircleCount counter (THIS IS AN ACCUMULATOR)

inCircleCount = 0

# Loop for numberDarts

for i in range(0, numberDarts):

# Get random coordinate and position dart

x = random.random() * 2 - 1

y = random.random() * 2 - 1

# Draw red dot AND INCREMENT COUNTER if in circle, blue dot if not

if inCircle(x, y) == True:

inCircleCount = inCircleCount + 1

drawDot(grapher, x, y, "red")

else:

drawDot(grapher, x, y, "blue")

# return approximation of pi

return float(inCircleCount) / numberDarts * 4

#Performs Monte Carlo simulation to generate approximation of Pi

def main():

# Get number of darts for simulation from user

# Note continuation character <\> so we don‘t go over 78 columns:

print("This is a program that simulates throwing darts at a dartboard\n" \

"in order to approximate pi: The ratio of darts in a unit circle\n"\

"to the total number of darts in a 2X2 square should be\n"\

"approximately  equal to pi/4")

#Create window, turtle, set up window as dartboard

window = turtle.Screen()

t = turtle.Turtle()

setUpDartboard(window, t)

PI = montePi( NUMBER_DARTS, t )

# Include the following code in order to update animation periodically

# instead of for each throw:

window.tracer(500)

#Conduct simulation and print result

print "Pi is approximately equal to " + str(PI)

#Keep the window up until dismissed

window.exitonclick()

main()

如图:

时间: 2024-08-28 23:39:49

python turtle,random,math的相关文章

python之random模块

random模块 用于生成随机浮点数.整数.字符串和随机抽取元素 方法: random()  生成一个随机浮点数,范围在0.0~1.0之间 uniform(上限,下限)  在设置的范围内,随机生成一个浮点数(上下限可以是整数,浮点数) randint(上限,下限)  在设定的范围内,随机生成一个整数(上下限必须为整数) choice(序列)  从任何序列中选取一个随机的元素返回 shuffle(序列)  随机打乱一个序列中元素的顺序 sample(序列,长度)  从指定的序列中随机截取指定长度的

【转载】python 模块 - random生成随机数模块

http://blog.csdn.net/pipisorry/article/details/39086463 随机数种子 要每次产生随机数相同就要设置种子,相同种子数的Random对象,相同次数生成的随机数字是完全相同的: random.seed(1) 这样random.randint(0,6, (4,5))每次都产生一样的4*5的随机矩阵 关于种子的介绍可参见[Java - 常用函数Random函数] Python标准库random模块 (生成随机数模块) random.random() r

ZH奶酪:【Python】random模块

Python中的random模块用于随机数生成,对几个random模块中的函数进行简单介绍.如下:random.random() 用于生成一个0到1的随机浮点数.如: import random random.random() 输出: 0.3701787746508932 random.uniform(a,b) 用于生成一个指定范围内的随机浮点数,两个参数一个是上线,一个是下线.如: random.uniform(10,20) 输出: 16.878776709127855 random.rand

Python随机数random模块学习,并实现生成6位验证码

一.前言 学习python随机数random模块的使用 ,并使用模块中的函数,实现6位验证码生成 二.random模块 1.random.random() 返回0-1直接的随机数,类型为float >>>print(random.random()) 0.1259184691662908 2.random.randint(1, 8) 返回1-8直接的随机数,包括8 >>>print(random.randint(1, 8)) 3 3.random.choice() 从一个

python的random模块(生成验证码)

python的random模块(生成验证码) random模块常用方法 random.random() #生成0到1之间的随机数,没有参数,float类型 random.randint(1, 3) #生成参数1到参数2之间的随机数,输出为int类型,[1,3] random.randrange(1, 3) #生成参数1到参数2之间的随机数,输出为int类型,[1,3),这个方法还有一种用法,就是下面介绍的这种 random.randrange(0,100,2) #从指定范围内,按指定基数递增的集

python基础知识进阶(四) Python语言的math库和random库和实例

元组和列表 写的 过程中没保存,下次补上吧.好尴尬,手好残. 计算机是一个确定性设备,不能产生真正的随机数.(使用seed函数,两次的值都是一样的) 由计算机产生的随机数,都是一个由种子产生的伪随机数列.相同的随机种子会产生相同的伪随机数列. π的计算 圆周率π是一个无理数,没有任何一个精确公式能够计算π值,π的计算只能采用近似算法. 国际公认的π值计算采用蒙特卡洛方法. 简单说,蒙特卡洛是利用随机试验求解问题的方法. π计算问题的ipo表示如下: 输入:抛点的数量 处理:对于每个抛洒点,计算点

python模块2 math\random\re\time\datetime模块

知识内容: 1.math模块 2.random模块 3.re模块 4.time模块 5.datetime模块 一.math模块 1.math模块的作用:  它提供了一些基本的数学运算的函数,比如说对数.三角函数.开发.乘方等 2.math模块中的内容 1 >>> import math 2 >>> dir(math) 3 ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'a

python的random模块

As an example of subclassing, the random module provides the WichmannHill class that implements an alternative generator in pure Python. The class provides a backward compatible way to reproduce results from earlier versions of Python, which used the

Paint a leaf with python turtle

参考博文: http://biancheng.dnbcw.info/python/443280.html https://docs.python.org/2/library/turtle.html?highlight=turtle#turtle.speed 效果图: code: import turtle as tt import numpy as ny import random x = ny.array([[.5],[.5]]) p = [.85,.92,.99,1.00] A1 = ny.