本文整理汇总了Python中random.uniform函数的典型用法代码示例。如果您正苦于以下问题:Python uniform函数的具体用法?Python uniform怎么用?Python uniform使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了uniform函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: genRandSubIntervals
def genRandSubIntervals():
intervals = []
for i in range(10):
start = random.uniform(0.0, 1.0)
end = random.uniform(start, 1.0)
intervals.append((start, end))
return intervals
示例2: splitter
def splitter():
splitField = ["ra", "dec", "dist", "mag", "absmag", "x", "y", "z", "vx", "vy", "vz"][random.randint(0, 10)]
if splitField == "ra":
splitValue = random.uniform(1, 23)
elif splitField == "dec":
splitValue = random.uniform(-87, 87)
elif splitField == "dist":
splitValue = math.exp(random.gauss(5.5, 1))
elif splitField == "mag":
splitValue = random.gauss(8, 1)
elif splitField == "absmag":
splitValue = random.gauss(2, 2)
elif splitField == "x":
splitValue = math.exp(random.gauss(5, 1)) * (1 if random.randint(0, 1) == 1 else -1)
elif splitField == "y":
splitValue = math.exp(random.gauss(5, 1)) * (1 if random.randint(0, 1) == 1 else -1)
elif splitField == "z":
splitValue = math.exp(random.gauss(5, 1)) * (1 if random.randint(0, 1) == 1 else -1)
elif splitField == "vx":
splitValue = math.exp(random.gauss(-12, 1)) * (1 if random.randint(0, 1) == 1 else -1)
elif splitField == "vy":
splitValue = math.exp(random.gauss(-12, 1)) * (1 if random.randint(0, 1) == 1 else -1)
elif splitField == "vz":
splitValue = math.exp(random.gauss(-12, 1)) * (1 if random.randint(0, 1) == 1 else -1)
return splitField, splitValue
示例3: init_random_generation
def init_random_generation(items):
generation = []
for i in range(items):
theta = random.uniform(15, 180) * math.pi / 180
v = random.uniform(2, 20)
generation.append((theta, v))
return generation
示例4: direct_pi
def direct_pi(N):
n_hits = 0
for i in range(N):
x, y = random.uniform(-1.0, 1.0), random.uniform(-1.0, 1.0)
if x ** 2 + y ** 2 < 1.0:
n_hits += 1
return n_hits
示例5: randPayoff
def randPayoff():
global M
global neighborhood
global neighbSize
# Randomizing Payoff
for x in range(0, len(ch)):
playingZeroMax = 0
playingOneMax = 0
playingZeroMin = 0
playingOneMin = 0
# Randomizing Payoff
for y in range(0, neighbSize[x]):
M[(x, neighborhood[x][y])][0][0] = round(random.uniform(0.0, 1.0), 3)
M[(x, neighborhood[x][y])][0][1] = round(random.uniform(0.0, 1.0), 3)
playingZeroMax += max(M[(x, neighborhood[x][y])][0][0], M[(x, neighborhood[x][y])][0][1])
playingZeroMin += min(M[(x, neighborhood[x][y])][0][0], M[(x, neighborhood[x][y])][0][1])
M[(x, neighborhood[x][y])][1][0] = round(random.uniform(0.0, 1.0), 3)
M[(x, neighborhood[x][y])][1][1] = round(random.uniform(0.0, 1.0), 3)
playingOneMax += max(M[(x, neighborhood[x][y])][1][0], M[(x, neighborhood[x][y])][1][1])
playingOneMin += min(M[(x, neighborhood[x][y])][1][0], M[(x, neighborhood[x][y])][1][1])
playingMax = max(playingOneMax, playingZeroMax)
playingMin = min(playingOneMin, playingZeroMin)
# Normalizing Payoff Matrix
for y in range(0, neighbSize[x]):
M[(x, neighborhood[x][y])][0][0] = round((M[(x, neighborhood[x][y])][0][0]-playingMin/neighbSize[x])/(playingMax-playingMin/neighbSize[x]), 3)
M[(x, neighborhood[x][y])][0][1] = round((M[(x, neighborhood[x][y])][0][1]-playingMin/neighbSize[x])/(playingMax-playingMin/neighbSize[x]), 3)
M[(x, neighborhood[x][y])][1][0] = round((M[(x, neighborhood[x][y])][1][0]-playingMin/neighbSize[x])/(playingMax-playingMin/neighbSize[x]), 3)
M[(x, neighborhood[x][y])][1][1] = round((M[(x, neighborhood[x][y])][1][1]-playingMin/neighbSize[x])/(playingMax-playingMin/neighbSize[x]), 3)
示例6: _genRhoWidth
def _genRhoWidth(psr):
"""Calculate the opening angle of pulsar, and the beamwidth.
Based on model outlined in Smits et al. 2009"""
# cut off period for model
perCut = 30.0
# calclate rho
randfactor = random.uniform(-.15, .15)
if psr.period > perCut:
rho = _rhoLaw(psr.period)
else:
rho = _rhoLaw(perCut)
logrho = math.log10(rho) + randfactor
rho = 10. ** logrho
# generate beta and pulse width
beta = random.uniform(-1, 1) * rho
width = _sindegree(0.5 * rho) * _sindegree(0.5 * rho)
width = width - (_sindegree(0.5 * beta) * _sindegree(0.5 * beta))
width = width / (_sindegree(psr.alpha) * _sindegree(psr.alpha + beta))
if width < 0.0 or width > 1.0:
width = 0.0
rho = 0.0
else:
width = math.sqrt(width)
# convert the width into degrees 0 -> 360 (ie. 90*4)
width = math.degrees(math.asin(width))*4.0
return rho, width
示例7: insertHapticSensorsRandom
def insertHapticSensorsRandom(self):
"""insert haptic sensors at random locations"""
self.sensorGroupName = 'haptic'
for _ in range(5):
self.insertHapticSensor(dx=random.uniform(-0.65, 0.65), dz=random.uniform(-0.4, 0.2))
##self.insertHapticSensor(dx=-0.055)
return
示例8: monte_carlo
def monte_carlo(beta, cubic, quartic):
beta = 2.0
N = 2 ** 5
dtau = beta / N
delta = 1.0
n_steps = int(10 ** 7)
X = np.zeros([n_steps, N])
x = [0.0] * N
for step in range(n_steps):
k = random.randint(0, N - 1)
knext, kprev = (k + 1) % N, (k - 1) % N
x_new = x[k] + random.uniform(-delta, delta)
old_weight = (
rho_free(x[knext], x[k], dtau) * rho_free(x[k], x[kprev], dtau) * math.exp(-dtau * V(x[k], cubic, quartic))
)
new_weight = (
rho_free(x[knext], x_new, dtau)
* rho_free(x_new, x[kprev], dtau)
* math.exp(-dtau * V(x_new, cubic, quartic))
)
if random.uniform(0.0, 1.0) < new_weight / old_weight:
x[k] = x_new
X[step, :] = x
if step % 10000 == 0:
print("step %d / %d" % (step, n_steps))
return X
示例9: test_validate_point_count_called
def test_validate_point_count_called(self):
import random
with mock.patch("course.page.base.validate_point_count")\
as mock_validate_point_count,\
mock.patch("course.page.base.get_auto_feedback")\
as mock_get_auto_feedback:
mock_validate_point_count.side_effect = lambda x: x
mock_get_auto_feedback.side_effect = lambda x: x
for i in range(10):
correctness = random.uniform(0, 15)
feedback = "some feedback"
AnswerFeedback(correctness, feedback)
mock_validate_point_count.assert_called_once_with(correctness)
# because feedback is not None
self.assertEqual(mock_get_auto_feedback.call_count, 0)
mock_validate_point_count.reset_mock()
for i in range(10):
correctness = random.uniform(0, 15)
AnswerFeedback(correctness)
# because get_auto_feedback is mocked, the call_count of
# mock_validate_point_count is only once
mock_validate_point_count.assert_called_once_with(correctness)
mock_validate_point_count.reset_mock()
# because feedback is None
self.assertEqual(mock_get_auto_feedback.call_count, 1)
mock_get_auto_feedback.reset_mock()
AnswerFeedback(correctness=None)
mock_validate_point_count.assert_called_once_with(None)
示例10: add_exhaust_to_face
def add_exhaust_to_face(bm, face):
if not face.is_valid:
return
# The more square the face is, the more grid divisions it might have
num_cuts = randint(1, int(4 - get_aspect_ratio(face)))
result = bmesh.ops.subdivide_edges(bm,
edges=face.edges[:],
cuts=num_cuts,
fractal=0.02,
use_grid_fill=True)
exhaust_length = uniform(0.1, 0.2)
scale_outer = 1 / uniform(1.3, 1.6)
scale_inner = 1 / uniform(1.05, 1.1)
for face in result['geom']:
if isinstance(face, bmesh.types.BMFace):
if is_rear_face(face):
face.material_index = Material.hull_dark
face = extrude_face(bm, face, exhaust_length)
scale_face(bm, face, scale_outer, scale_outer, scale_outer)
extruded_face_list = []
face = extrude_face(bm, face, -exhaust_length * 0.9, extruded_face_list)
for extruded_face in extruded_face_list:
extruded_face.material_index = Material.exhaust_burn
scale_face(bm, face, scale_inner, scale_inner, scale_inner)
示例11: testSolveAndCall
def testSolveAndCall( self ) :
random.seed( 0 )
for i in range( 0, 100 ) :
s = IECore.Splineff()
x = 0
for i in range( 0, 40 ) :
s[x] = random.uniform( 0, 10 )
x += 1 + random.uniform( 0, 1 )
xv = s.keys()
yv = s.values()
for i in range( 0, 1000 ) :
# select a segment
seg = int(random.uniform( 0, int(len(xv) / 4) ))
seg -= seg % s.basis.step
# evaluate an x,y point on the curve directly
# ourselves
t = i / 1000.0
c = s.basis.coefficients( t )
x = xv[seg+0] * c[0] + xv[seg+1] * c[1] + xv[seg+2] * c[2] + xv[seg+3] * c[3]
y = yv[seg+0] * c[0] + yv[seg+1] * c[1] + yv[seg+2] * c[2] + yv[seg+3] * c[3]
# then check that solving for x gives y
yy = s( x )
self.assertAlmostEqual( yy, y, 3 )
示例12: generate_asteroid
def generate_asteroid(self, now):
# Check if it's time to create a new object
if (now > self.next_gen_time):
# Create a new object
# Select a random item from the list, and
# pull it out of the list
# Note: It should have been cloned, but there is a problem
# with clone()
ast_num = random.randint(0,len(self.asteroid_model_list)-2)
nobj = self.asteroid_model_list[ast_num]
del self.asteroid_model_list[ast_num]
# Select an incident angle and speed
azimuth = random.uniform(*AZIMUTH_RANGE)
incl = random.uniform(*INCLINATION_RANGE)
speed = random.uniform(*SPEED_RANGE)
# Create the asteroid object
ast = Asteroid(nobj, azimuth, incl, speed, now, self.explosion_shader, self.regular_shader)
# Calculate the next generation time
self.calc_next_gen_time()
# Return the new asteroid
return ast
else:
# Do not create anything
return None
示例13: __init__
def __init__(self, pos):
self.pos = pos
self.life = 10 + int(random.random() * 2)
self.move = Vec2D(random.uniform(-2.5, 2.5), random.uniform(-2.5, 0.0))
self.surf = resman.get("game.sparkle_surf")
width, height = self.surf.get_size()
self.center = Vec2D(width / 2, height / 2)
示例14: test_geopoint_to_native
def test_geopoint_to_native():
geo = GeoPointType(required=True)
with pytest.raises(ConversionError):
native = geo.to_native((10,))
with pytest.raises(ConversionError):
native = geo.to_native({'1':'-20', '2': '18'})
with pytest.raises(ConversionError):
native = geo.to_native(['-20', '18'])
with pytest.raises(ConversionError):
native = geo.to_native('-20, 18')
class Point(object):
def __len__(self):
return 2
with pytest.raises(ConversionError):
native = geo.to_native(Point())
native = geo.to_native([89, -12])
assert native == [89, -12]
latitude = random.uniform(-90, 90)
longitude = random.uniform(-180, 180)
point = [latitude, longitude]
native = geo.to_native(point)
assert native == point
示例15: draw_stochastic2
def draw_stochastic2(s):
return draw_generic(RhinoTurtle(), {
'a': lambda t: t.forward(random.uniform(10,20)),
'b': lambda t: t.right(random.uniform(0, 90)),
'c': lambda t: t.left(random.uniform(0, 90)),
}, s)