我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用random.gammavariate()。
def test_zeroinputs(self): # Verify that distributions can handle a series of zero inputs' g = random.Random() x = [g.random() for i in range(50)] + [0.0]*5 g.random = x[:].pop; g.uniform(1,10) g.random = x[:].pop; g.paretovariate(1.0) g.random = x[:].pop; g.expovariate(1.0) g.random = x[:].pop; g.weibullvariate(1.0, 1.0) g.random = x[:].pop; g.vonmisesvariate(1.0, 1.0) g.random = x[:].pop; g.normalvariate(0.0, 1.0) g.random = x[:].pop; g.gauss(0.0, 1.0) g.random = x[:].pop; g.lognormvariate(0.0, 1.0) g.random = x[:].pop; g.vonmisesvariate(0.0, 1.0) g.random = x[:].pop; g.gammavariate(0.01, 1.0) g.random = x[:].pop; g.gammavariate(1.0, 1.0) g.random = x[:].pop; g.gammavariate(200.0, 1.0) g.random = x[:].pop; g.betavariate(3.0, 3.0) g.random = x[:].pop; g.triangular(0.0, 1.0, 1.0/3.0)
def _fix(self): return int(round(random.gammavariate(self.alpha, self.beta)))
def sample(self): return random.gammavariate(self.k, self.theta)
def rvs(self, size=1): res = np.zeros(shape=(size, self._K)) for i in range(size): alpha = np.array([random.gammavariate(self._a, 1. / self._b) for local in range(self._K)]) try: res[i,:] = np.random.dirichlet(alpha, 1) except: res[i, :] = res[i-1,:] # res[i, :] = np.array([random.gammavariate(local, 1) for local in alpha]) return res
def wishrnd(sigma, v_0, C=None): """Return a sample from a Wishart distribution.""" if C == None: C = np.linalg.cholesky(sigma) D = sigma.shape[0] a = np.zeros((D, D), dtype=np.float32) for r in xrange(D): if r != 0: a[r, :r] = np.random.normal(size=(r,)) a[r, r] = math.sqrt(random.gammavariate(0.5*(v_0 - D + 1), 2.0)) return np.dot(np.dot(np.dot(C, a), a.T), C.T)
def one(o): return o.final(random.gammavariate(o.a, o.b))
def wishartrand(self, nu, sigma, C=None): """Return a sample from a Wishart distribution.""" if C == None: C = np.linalg.cholesky(sigma) D = sigma.shape[0] a = np.zeros((D, D), dtype=np.float32) for r in xrange(D): if r != 0: a[r, :r] = np.random.normal(size=(r,)) a[r, r] = np.sqrt(random.gammavariate(0.5*(nu - D + 1), 2.0)) return np.dot(np.dot(np.dot(C, a), a.T), C.T)
def test_gammavariate_errors(self): # Both alpha and beta must be > 0.0 self.assertRaises(ValueError, random.gammavariate, -1, 3) self.assertRaises(ValueError, random.gammavariate, 0, 2) self.assertRaises(ValueError, random.gammavariate, 2, 0) self.assertRaises(ValueError, random.gammavariate, 1, -3)
def bandwidth(self, alpha=1.0, beta=0.5, bandwidth_max=100000): """Completely make-believe bandwith. It's calculated as a random point on the probability density function of a gamma distribution over (0,100000] in KB/s. """ if not self._bandwidth: self._bandwidth = \ int(floor(random.gammavariate(alpha, beta) * bandwidth_max)) return self._bandwidth
def pwntime(self, alpha=1.0, beta=0.5, median=60*60*24*3): """The amount of time it takes to pwn this node. :ivar median: The median amount of time it should take to own this node. By default, three days. (It takes the skids a while to figure out which metasploit module to use.) """ if not self._pwntime: self._pwntime = int(floor(random.gammavariate(alpha, beta) * median)) return self._pwntime
def constantFactory(constants, pset): for const_block in constants: if(not const_block.has_key("type")): raise ValueError("Constant block in configuration missing type key.\n %s" % str(const_block)); typename = const_block["type"].lower() if typename == "randint": if(not const_block.has_key("min") or not const_block.has_key("max")): raise ValueError("Constant randint in configuration must have min and max values.\n %s" % str(const_block)); minn = int(const_block["min"]) maxx = int(const_block["max"]) pset.addEphemeralConstant("randint%d.%d" %(minn, maxx), lambda: random.randint(minn,maxx)) elif typename == "uniform": if(not const_block.has_key("min") or not const_block.has_key("max")): raise ValueError("Constant uniform in configuration must have min and max values.\n %s" % str(const_block)); minn = int(const_block["min"]) maxx = int(const_block["max"]) pset.addEphemeralConstant("uniform%d.%d" %(minn, maxx), lambda: random.uniform(minn,maxx)) elif typename == "normal": if(not const_block.has_key("mu") or not const_block.has_key("sigma")): raise ValueError("Constant normal in configuration must have mu and sigma values.\n %s" % str(const_block)); mu = int(const_block["mu"]) sigma = int(const_block["sigma"]) pset.addEphemeralConstant("normal%d.%d" %(mu, sigma), lambda: random.normalvariate(mu,sigma)) elif typename == "gamma": if(not const_block.has_key("alpha") or not const_block.has_key("beta")): raise ValueError("Constant gamma in configuration must have alpha and beta values.\n %s" % str(const_block)); alpha = int(const_block["alpha"]) beta = int(const_block["beta"]) pset.addEphemeralConstant("normal%d.%d" %(alpha, beta), lambda: random.gammavariate(alpha,beta)) elif typename == "constant": if(not const_block.has_key("value")): raise ValueError("Constant constant in configuration must have value.\n %s" % str(const_block)); value = int(const_block["value"]) pset.addEphemeralConstant("const%d" %(value), lambda: value) else: raise ValueError("Unknown constant type %s\n" % typename)
def _execute(self, sources, alignment_stream, interval): if alignment_stream is None: raise ToolExecutionError("Alignment stream expected") for ti, _ in alignment_stream.window(interval, force_calculation=True): yield StreamInstance(ti, random.gammavariate(alpha=self.alpha, beta=self.beta))