python创建6个随机数

236 阅读1分钟

代码块

    red_balls = [x for x in range(1, 34)]
    selected_balls = sample(red_balls, 6)
    selected_balls.sort()
    selected_balls.append(randint(1, 16))
    return selected_balls

[x for x in range(1, 34)] 意为创建一个1到33之间的整数列表。

sample(red_balls, 6)从序列中取6个元素。

sort()对列表排序。

randint()取随机数。

append()在列表末尾添加新元素。