繁体   English   中英

按行输出随机数

[英]Output random numbers by line

好了,所以我编写了以下代码:

lst1 = tuple(range(1, 13))
table1 = ""
x = 0
while x < len(lst1):
    for y in range(0, 3):
        table1 += str(lst1[x]) + "\t"
        x += 1
    table1 += "\n"
print(table1)

#Output in console is:
1 2 3
4 5 6
7 8 9
10 11 12

我想再创建2个显示其他随机数的表,即:从0到48可以说,但仍然只能以该格式输出该范围内的12个数字。 我是python的新手,似乎无法通过random模块弄清楚。

这是带有随机数的列表之一:

lst3 = tuple(range(0, 48))
table3 = ""
x = 0
while x < len(lst3):
    for y in range(0, 3):
        table3 += str(lst3[x]) + "\t"
        x += 1
    table3 += "\n"
print(random.sample(lst3, 12))

#Output is: (so basically just 12 random numbers from 1 to 47 that don't repeat)
[28, 15, 35, 11, 30, 20, 38, 3, 31, 42, 9, 24]

根据我的理解,您需要这样的东西:

import random

lst1 = tuple(range(1, 13))
lst2 = random.sample(range(0,48), 12) # increase this 12 as per your requirements
table1 = ""
table2 = ""
x = 0
while x < len(lst1):
    for y in range(0, 3):
        table1 += str(lst1[x]) + "\t"
        x += 1
    table1 += "\n"
x = 0
while x < len(lst2):
    for y in range(0, 3):
        table2 += str(lst2[x]) + "\t"
        x += 1
    table2 += "\n"

print(table1)
print (table2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM