Uli*_*vez -1 python windows random math console
我尝试制作一个代码,在文本中打印两个相等的随机值。
import random
archivo=open('Ejercicio3.txt', 'w')
up=0
insert=int(input('Valor: '))
for i in range(insert):
up+=1
n1=random.randint(10, 100)
n2=random.randint(10, 100)
archivo.write(f'{up}.- {n1} - {n2} = {n1-n2}\n\n')
print(f'{up}.- {n1} - {n2} = {n1-n2}')
archivo.write(f'{n1-n2}\n')#This is the problem, because the code write numbers different random
archivo.close()
Run Code Online (Sandbox Code Playgroud)
我想要的是它做类似的事情:
import random
archivo=open('Ejercicio1.1.txt', 'w')
up=0
insert=int(input('Valor: '))
for i in range(insert):
up+=1
n1=random.randint(10, 100)
n2=random.randint(10, 100)
archivo.write(f'{n1} - {n2} =''\n\n')
up=0
archivo.write('---------------------------------------------\n')
for i in range(insert):
up+=1
n1=random.randint(10, 100)
n2=random.randint(10, 100)
archivo.write(f'{n1-n2}''\n\n')#Except this part, I want it to be exactly the same as the result of the subtraction above.
archivo.close()
Run Code Online (Sandbox Code Playgroud)
我想要的结果是: 示例
有人可以帮助我吗?非常感谢。问候
小智 5
您可以使用random.seed()和设置相同的种子两次。
import random
random.seed(0)
print(random.random())
#prints a random number
random.seed(0)
print(random.random())
#prints the same random number
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
73 次 |
| 最近记录: |