def addBunnies(self, num=500): data = {"components":[ { "type":"Sprite", "file":u"images/wabbit_alpha.png", "x": 0, "y": 0 }, ]} r = Random() for x in range(0,num): bunny = Entity.create(id = 'bunny %d' % x, scene=self, **data) if self.firstBunny is None: self.firstBunny = bunny if self.lastBunny is not None: self.lastBunny.nextBunny = bunny bunny.x = r.random()*self.size['width'] bunny.y = self.size['height'] bunny.speedx = int(r.random()*70.0) - 35 bunny.speedy = int(r.random()*70.0) bunny.angle = 90 - r.random() * 90 bunny.z = int(r.random()*100) bunny.nextBunny = None self.lastBunny = bunny self.entities[bunny.id] = bunny Gilbert().startEntity(bunny) self.nBunnies+=num fps = self.getComponent("fps") if fps != None: fps.text = str(self.nBunnies)
def assignList (self): if len(Player.usedMoveArrayIndices) == 4: Player.usedMoveArrayIndices.clear() rand = Random() randomValue = int(rand.random() * 4) while Player.usedMoveArrayIndices.__contains__(randomValue): randomValue = int(rand.random() * 4) Player.usedMoveArrayIndices.add(randomValue) self.moves = Player.moveArray[randomValue]
def manufactureLists(): # for i in range(0,Player.moveB.len()): # Player.moveB.(i) = MoveEntry.MoveEntry(move=i,points = 100) rand = Random() for i in range (0,len(Player.moveArray)): tempL = Player.moveArray[i] for j in range(0,len(tempL)): tempL[j] = MoveEntry(move=tempL[j], points = 100*rand.random())
def gen_keys(): r = Random() min_p = (10**4) * (1 + 100 * r.random()) min_q = (10**4) * (1 + 100 * r.random()) p = first_prime_after(min_p) q = first_prime_after(min_q) T = (p - 1) * (q - 1) N = p * q E = 65537 #mcd(E,T)==1 and E < T D = modinv(E, T) #D*E = 1 (mod T) return dict(public=[E, N], private=[D, N])
class LearningCurve(Classification): def __init__(self, classifierName, classifierArgs, numFolds=10, parallel=1, metric='roc_auc', getCV=None, preDispatch='2*n_jobs', classifyHidden=False, steps=10, seed=2): super(LearningCurve, self).__init__(classifierName, classifierArgs, numFolds, parallel, metric, getCV, preDispatch, classifyHidden) self.steps = steps self.random = Random(seed) self.thresholds = None self.currentCutoff = None self.saveExtra = False def subsample(self, array, thresholds, cutoff): indices = [] assert array.shape[0] == len(thresholds) for i in range(len(thresholds)): if thresholds[i] < cutoff: indices.append(i) return array[indices] def _getResult(self, setName, classifier, cv, params, score=None, fold=None, mean_score=None, scores=None, extra=None): result = super(LearningCurve, self)._getResult(setName=setName, classifier=classifier, cv=cv, params=params, score=score, fold=fold, mean_score=mean_score, scores=scores, extra=extra) result["cutoff"] = self.currentCutoff result["step"] = self.currentStep return result def _insert(self, tableName, rows): if tableName == "result": tableName = "learning_curve" super(LearningCurve, self)._insert(tableName, rows) def classify(self): self.indices, X_train, X_hidden, y_train, y_hidden = self._splitData() thresholds = [self.random.random() for x in y_train] for i in range(self.steps): print "----------------------", "Learning curve step", i + 1, "----------------------" self.currentCutoff = float(i + 1) / self.steps self.currentStep = i print "Cutoff", self.currentCutoff y_sample = self.subsample(y_train, thresholds, self.currentCutoff) X_sample = self.subsample(X_train, thresholds, self.currentCutoff) if "class" in self.meta.db.tables: print "Classes y_sample = ", countUnique(y_sample) search = self._crossValidate(y_sample, X_sample, self.classifyHidden and (X_hidden.shape[0] > 0)) if self.classifyHidden: self._predictHidden(y_hidden, X_hidden, search, y_sample.shape[0]) self.currentCutoff = None self.indices = None
def test_argsort_random(self): from numpy import array from _random import Random rnd = Random(1) a = array([rnd.random() for i in range(512*2)]).reshape(512,2) a.argsort()
def predict(self, x_test): randObj = Random() retArr = [] for _ in range(0,len(x_test)): retArr.append(int(randObj.random()*3)) return numpy.asarray(retArr)
class Ball : def __init__(self,x, y, vx, vy): self.rand=None self.model = {"walls" : {"left":0, "top":0, "right": 500, "bottom": 300}, "elastity" : -0.2, "ballRadius" : 26, "maxSpeed" : 3.0 } # default provisioning self.rand=Random() if (x == None) : Threading.Thread.Sleep(15) x = (self.model["walls"]["right"] - self.model["walls"]["left"] - 2*self.model["ballRadius"])* self.rand.random() y = (self.model["walls"]["bottom"] - self.model["walls"]["top"] - 2*self.model["ballRadius"])* self.rand.random() vx = 2*self.model["maxSpeed"]* self.rand.random() - self.model["maxSpeed"] vy = 2*self.model["maxSpeed"]* self.rand.random() - self.model["maxSpeed"] self._x = x self._y = y self._vx = vx self._vy = vy self._r = self.model["ballRadius"] # d = 52 px self._d = 2*self._r self._d2 = self._d*self._d def Ballmove(self): self._x +=self._vx self._y += self._vy if (self._x < self.model["walls"]["left"] and self._vx<0): #self._vx += (self._x - walls.left)*elastity self._vx = -self._vx # top if (self._y < self.model["walls"]["top"] and self._vy<0): #self._vy += (self._y - walls.top)*elastity self._vy = -self._vy # left if (self._x > self.model["walls"]["right"] - self._d and self._vx>0): #self._vx += (self._x - walls.right + self._d)*elastity self._vx = -self._vx # top if (self._y > self.model["walls"]["bottom"] - self._d and self._vy>0) : #self._vy += (self._y - walls.bottom + self._d)*elastity self._vy = -self._vy def doCollide(self,b): dx = self._x - b._x dy = self._y - b._y dvx = self._vx - b._vx dvy = self._vy - b._vy distance2 = dx*dx + dy*dy if (Math.Abs(dx) > self._d or Math.Abs(dy) > self._d): return False if (distance2 > self._d2): return False # make absolutely elastic collision mag = dvx*dx + dvy*dy # test that balls move towards each other if (mag > 0): return False mag /= distance2 delta_vx = dx*mag delta_vy = dy*mag self._vx -= delta_vx self._vy -= delta_vy b._vx += delta_vx b._vy += delta_vy return True