def _f4(self): from random import randint as r # 直接导入randint函数,更名为r from random import uniform as ru # 直接导入uniform函数,更名为ru,用于生成指定范围内的随机浮点数 from fractions import Fraction as f # 直接导入fractions函数,更名为f ops = ['+', '-', '*', '/'] # 存储操作符 kuohao = ['(', '', ')'] # 存储括号,下标为0,1,2 left1 = r(0, 1) left2 = r(0, 1) left3 = r(0, 1) right1 = r(1, 2) right2 = r(1, 2) right3 = r(1, 2) if left1 == 0: left2 = 1 left3 = 1 if right1 == 2: right2 = 1 right3 = 1 else: right2 = 2 right3 = 1 else: if left2 == 0: left3 = 1 right1 = 1 if right2 == 2: right3 = 1 else: right3 = 2 else: left3 = 0 right1 = 1 right2 = 1 right3 = 2 add_1 = ru(0, 1) add_1 = f(add_1).limit_denominator(10) # 限制最大分母值,小数变分数 add_2 = ru(0, 1) add_2 = f(add_2).limit_denominator(10) add_3 = r(1, 10) add_4 = r(1, 10) ops1 = r(0, 2) ops2 = r(0, 3) ops3 = r(0, 3) # 由上述操作,随机生成表达式 eq = kuohao[left1] + str(add_1) + ops[ops1] + kuohao[left2] + str(add_2) + kuohao[right1] + ops[ops2] + kuohao[ left3] + str(add_3) + kuohao[right2] + ops[ops3] + str(add_4) + kuohao[right3] return (eq)
def mutate(self, mutate_probability): l = list(self.rep) for i in xrange(self.sig_size): r = ru(0,1) if r < mutate_probability: for j in xrange(i,self.sig_size, self.sig_size): if l[j] == '1': l[j] = '0' elif l[j] == '0': l[j] = '1' self.rep = "".join(l)
def BuildField(wi,depth,inc,points,width,npoints) : xmin=-wi/2.0 xmax=wi/2.0 zmin=-depth/2.0 zmax=depth/2.0 pappend=points.append wappend=width.append npappend=npoints.append random.seed(1) ru=random.uniform zpos=zmin plus=0.1 minus=-0.1 while(zpos < zmax ) : xpos=xmin while (xpos < xmax) : pappend(xpos+ru(minus,plus)) pappend(0) pappend(zpos+ru(minus,plus)) pappend(xpos+ru(minus,plus)) pappend(0.1) pappend(zpos+ru(minus,plus)) pappend(xpos+ru(minus,plus)) pappend(0.2) pappend(zpos+ru(minus,plus)) pappend(xpos+ru(minus,plus)) pappend(0.3+ru(-0.1,0.1)) pappend(zpos+ru(minus,plus)) wappend(0.006) wappend(0.003) npappend(4) xpos+=inc zpos+=inc
def BuildField(wi, depth, inc, points, width, npoints): xmin = -wi / 2.0 xmax = wi / 2.0 zmin = -depth / 2.0 zmax = depth / 2.0 pappend = points.append wappend = width.append npappend = npoints.append random.seed(1) ru = random.uniform zpos = zmin plus = 0.1 minus = -0.1 while zpos < zmax: xpos = xmin while xpos < xmax: pappend(xpos + ru(minus, plus)) pappend(0) pappend(zpos + ru(minus, plus)) pappend(xpos + ru(minus, plus)) pappend(0.1) pappend(zpos + ru(minus, plus)) pappend(xpos + ru(minus, plus)) pappend(0.2) pappend(zpos + ru(minus, plus)) pappend(xpos + ru(minus, plus)) pappend(0.3 + ru(-0.1, 0.1)) pappend(zpos + ru(minus, plus)) wappend(0.006) wappend(0.003) npappend(4) xpos += inc zpos += inc
ri.TransformBegin() points = [] width = [] colour = [] normals = [] # get a pointer to the append method as it is faster than calling it # each time pappend = points.append wappend = width.append cappend = colour.append nappend = normals.append # ru is random.uniform brought in by the import statement above for i in range(0, 2000): for ix in range(0, 3): cappend(ru(0, 1)) pappend(ru(-2, 2)) nappend(ru(0, 1)) wappend(ru(0.01, 0.2)) ri.Pattern('colour', 'colourShader') ri.Bxdf('PxrDiffuse', 'bxdf', {'reference color diffuseColor': ['colourShader:Cout']}) ri.Points({ri.P: points, ri.CS: colour, ri.WIDTH: width, ri.N: normals}) ri.TransformEnd() ri.WorldEnd() # and finally end the rib file ri.End()
# Задача 1 from random import randint numbers = [] for i in range(20): numbers.append(randint(1, 101)) print(numbers) # Задача 2 from random import uniform as ru triangle = {x: [ru(1, 10.0), ru(1, 10.0)] for x in ["A","B", "C"]} print(triangle) # Задача 3 my_str = "Im the string" def func (my_str): print("***" + my_str + "***") func(my_str) # Задача 4 my_dict_1 = {"a":1, "b":2, "c":3, "d":4, "e":5} my_dict_2 = {"f":6, "a":7, "g":8, "b":9, "h":10} # a print(list(my_dict_1.keys() & my_dict_2.keys())) # б print(list(my_dict_1.keys() - my_dict_2.keys()))
ri.Translate(0,0,4) ri.TransformBegin() points=[] width=[] colour=[] normals=[] # get a pointer to the append method as it is faster than calling it # each time pappend=points.append wappend=width.append cappend=colour.append nappend=normals.append # ru is random.uniform brought in by the import statement above for i in range(0,2) : for ix in range(0,3) : cappend(ru(0,1)) pappend(ru(-2,2)) nappend(ru(0,1)) wappend(ru(0.01,0.2)) ri.Surface("plastic") ri.Points({ri.P:points,ri.CS:colour,ri.WIDTH:width,ri.N:normals}) ri.TransformEnd() ri.WorldEnd() # and finally end the rib file ri.End()
from datetime import datetime, timedelta from math import sqrt, log from random import uniform as ru results = list() N = 3 for i in range(20): N = ((N * 120) / 100) + 10 edge = int(sqrt(N) * 1.25) + 1 pairs = list( set([( int(ru(0, edge)), int(ru(0, edge)), ) for i in range(N)])) t0 = datetime.now() st = set() count = 0 while pairs: x, y = pairs.pop() for ox, oy in pairs: lsquared = (x - ox) * (x - ox) + (y - oy) * (y - oy) triple = (x + ox, y + oy, lsquared) if triple in st: count += 1 else: st.add(triple) results.append( (N, N * N, edge, (datetime.now() - t0).total_seconds(), count)) print(results[-1])