def timeTrials(f, n, trials): total = 0.0 a = stdarray.create1D(n, 0.0) for t in range(trials): for i in range(n): a[i] = stdrandom.uniformFloat(0.0, 1.0) watch = Stopwatch() f(a) total += watch.elapsedTime() return total
import stddraw import stdarray from turtle import Turtle # Accept as command-line arguments an integer turtleCount, an integer # stepCount, and a float stepSize. Create turtleCount Turtle objects, # and have them make stepCount random steps of size stepSize. turtleCount = int(sys.argv[1]) stepCount = int(sys.argv[2]) stepSize = float(sys.argv[3]) stddraw.setPenRadius(0.0) stddraw.clear(stddraw.LIGHT_GRAY) turtles = stdarray.create1D(turtleCount) for i in range(turtleCount): x = stdrandom.uniformFloat(0.0, 1.0) y = stdrandom.uniformFloat(0.0, 1.0) turtles[i] = Turtle(x, y, 0.0) for j in range(stepCount): for i in range(turtleCount): turtles[i].turnLeft(stdrandom.uniformFloat(0.0, 360.0)) turtles[i].goForward(stepSize) stddraw.show(0.0) stddraw.show() #----------------------------------------------------------------------- # python drunks.py 20 500 .005 # python drunks.py 20 1000 .005
from color import Color from picture import Picture # Read values from standard input to create an array of charged # particles. Set each pixel color in an image to a grayscale value # proportional to the total of the potentials due to the particles at # corresponding points. Draw the resulting image to standard draw. MAX_GRAY_SCALE = 255 # Read charges from standard input into an array. #n = stdio.readInt() n = eval(sys.argv[1]) charges = stdarray.create1D(n) for i in range(n): x0 = round(stdrandom.uniformFloat(0, 1), 2) y0 = round(stdrandom.uniformFloat(0, 1), 2) q0 = stdrandom.gaussian(50, 150) charges[i] = Charge(x0, y0, q0) print(str(charges[i])) # Create a Picture depicting potential values. pic = Picture() for col in range(pic.width()): for row in range(pic.height()): # Compute pixel color. x = 1.0 * col / pic.width() y = 1.0 * row / pic.height() v = 0.0 for i in range(n):
def exp(lamda): x = stdrandom.uniformFloat(0, 1) return -math.log(x / lamda)
hurstExponent = float(sys.argv[1]) a_brown_total = [] for i in range(20): stddraw.setPenRadius(0.0) a_brown = stdarray.create1D(44100, 0) beta = 2.0**(2.0 * hurstExponent) fill_brownian(a_brown, 0, 44099, 0.05, beta) a_brown_total = a_brown_total + a_brown a_total_white = stdarray.create1D(882000, 0) for i in range(len(a_total_white)): if (i % 44100) == 0: a_total_white[i] = 0 else: a_total_white[i] = stdrandom.uniformFloat(-0.25, 0.25) y = stdarray.create1D(882000, 0) for i in range(882000): f = 0.25 t = i / 44100 y[i] = ((1 - (math.sin(math.pi * f * t))**6) * a_brown_total[i]) + (( (math.sin(math.pi * t * f))**6) * a_total_white[i]) """ for i in range(len(y)): if i < len(y) - 1: stddraw.line(i,a_brown_total[i],i+1,a_brown_total[i+1]) """ stdaudio.playSample(y)
def pluck(self): for i in range(self.p): self.list[i] = stdrandom.uniformFloat(-0.5,0.5) self.deque = collections.deque(self.list) return self.deque
#----------------------------------------------------------------------- # drunk.py #----------------------------------------------------------------------- import sys import stdrandom import stddraw from turtle import Turtle #----------------------------------------------------------------------- # Accept as command-line arguments an integer stepCount specifying a # number of iterations, and a float stepSize specifying a step size. # Create a Turtle object, and have it make stepCount random steps of # size stepSize. stepCount = int(sys.argv[1]) stepSize = float(sys.argv[2]) stddraw.setPenRadius(0.0) stddraw.clear(stddraw.LIGHT_GRAY) myTurtle = Turtle(0.5, 0.5, 0.0) for i in range(stepCount): myTurtle.turnLeft(360.0 * stdrandom.uniformFloat(0.0, 360.0)) myTurtle.goForward(stepSize) stddraw.show(0.0) stddraw.show() #----------------------------------------------------------------------- # python drunk.py 10000 .01