# python随机生成20个0～100范围的数字_如何在Python中随机生成递减数字？

8 个答案:

import math

import random

def generate( min = 0, max = 10, number = 100 ):

start = 0

for i in xrange( number, 0, -1 ):

start = start + math.log( random.random( ) ) / i

next = math.exp( start ) * ( max - min ) + min

yield next

for number in generate( ):

print number

from random import random

min=0

max=10

oldval=1.

while True:

oldval=oldval*random()

randval=min+(max-min)*oldval

import random

random_range = range(10)

numbers = [random.choice(random_range[:i]) for i in range(10, 0, -1)]

import random

max = 10.0

min = 0.0

desired = 100

step = (max - min) / desired

numbers = [random.random() * (max - (i * step)) for i in range(desired)]

import random, numpy

max = 10.0

min = 0.0

desired = 100

step = float(min - max) / desired

window = 1.0

numbers = [x + (random.random() * window) - (window / 2.0) for x in numpy.arange(max, min, step)]

import random

i = 1000000

while i > 0:

i = random.randint(0, i)

print i

import random

random_decreasing_integers_from_range = (i for i in xrange(max, min - 1, -1) if random.random() > .5)

import random

def random_decreasing_integers_from_range(min, max, num_outputs):

range_size = abs(max - min)

if range_size < num_outputs:

raise ValueError('Distance from min to max must be equal to or greater than num_outputs.')

output_count = 0

for iteration, value in enumerate(xrange(max, min - 1, -1)):

# if we only have enough values left to satisfy the number requested,

# yield value

if num_outputs - output_count == range_size - iteration + 1:

output_count += 1

yield value

# else yield value randomly, weighted by how far in to the range we are

# and how many values we have left to yield of the total requested

else:

ratio_consumed = float(iteration + 1) / range_size

ratio_yielded = float(output_count) / num_outputs

if random.random() < (1 - ratio_yielded) * ratio_consumed:

output_count += 1

yield value

# if we've yielded the requested number of values, stop

if output_count == num_outputs:

break

a=10000

for i in range(1,50):

b=random.randint(1,a)

print(b)

a=b

import random

def generate():

n = 1.0

while n:

n = random.random() * n

yield n

iterator = generate()

iterator.next()

• 0
点赞
• 0
收藏
• 打赏
• 0
评论
06-20
07-20 6836
05-12 6515
04-02 662
02-25 876
10-27 6190
05-18 9812

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

weixin_39980575

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。