无法在 Python 中的后续行中迭代随机值
Unable to iterate random values in subsequent lines in Python
可能有许多更直接的方法可以实现我的目标,但作为 Python 新手,如果我们能专注于我正在使用的代码,我将不胜感激。
我正在尝试为图形程序 (G'MIC) 写出命令列表,并使用 Python 3.6.4 (OS: Windows 10 Pro),我需要写很多行代码,但每一行都必须有不同的数字集(整数和浮点数).. 例如,在我现在尝试使用的代码中,语法是这样的:
gmic v -99 input_.png fx_pastell
0.5,1,0,10,40,633,11,95,1,1,0.291651967558584,0,0,8,150,1,81,1,0,0,0 -o
out_001.png
^ 所有变量都表示特定脚本中的参数(在本例中为 Pastell 效果)。
我编写的代码依赖于 Random 模块为我想要更改的每个参数获取一定范围内的特定数字。
我的绊脚石是让脚本在每次打印一行时输出不同的随机数。这是我的代码(这很糟糕,我知道..):
import random
a = random.randint (3,13)
b = random.randint (1,68)
c = random.randint (1,682)
d = random.randint (2,12)
e = random.randint (1,109)
g = random.randint (1,8)
h = random.uniform (0, 1)
k = random.randint (1,11)
l = random.randint (1,201)
n = random.randint (1,300)
o = random.randint (1,4)
dataFile = open("gmic1.txt", "w")
for line in range(120):
dataFile.write("gmic v -99 input_.png fx_pastell 0.5,1,0" + "," + str(a)
+ "," + str(b) + "," + str(c) + "," +str(d) + "," + str(e) + ",1," + str(g)
+ "," + str(h) + ",0,0," + str(k) + "," + str(l) + ",1," + str(n) + "," +
str(o) +"," + "0,0,0 -o out_%04d.png \n" % line)
dataFile.close()
输出是这样的:
gmic v -99 input_.png fx_pastell
0.5,1,0,12,2,521,12,85,1,7,0.04003331068764937,0,0,8,17,1,297,2,0,0,0 -o
out_0000.png
gmic v -99 input_.png fx_pastell
0.5,1,0,12,2,521,12,85,1,7,0.04003331068764937,0,0,8,17,1,297,2,0,0,0 -o
out_0001.png
.. etc...
没有这样的错误,但没有像我希望的那样每次都输出一组不同的数字。
我可以用 Notepad++ 的列编辑器更改第一个输入图像名称变量,这样就可以解决这个问题,我只需要知道如何使每一行代码不同。
非常感谢任何帮助!
你应该在每次迭代时调用 rand 函数
from random import randint, uniform
dataFile = open("gmic1.txt", "w")
for line in range(120):
dataFile.write("gmic v -99 input_.png fx_pastell 0.5,1,0"
+ "," + str(randint(3, 13))
+ "," + str(randint(1, 68))
+ "," + str(randint(1, 682))
+ "," + str(randint(2, 12))
+ "," + str(randint(1, 109))
+ ",1," + str(randint(1, 8))
+ "," + str(uniform(0, 1))
+ ",0,0," + str(randint(1, 11))
+ "," + str(randint(1, 201))
+ ",1," + str(randint(1, 300))
+ "," + str(randint(1, 4))
+ "," + "0,0,0 -o out_%04d.png \n" % line)
dataFile.close()
可能有许多更直接的方法可以实现我的目标,但作为 Python 新手,如果我们能专注于我正在使用的代码,我将不胜感激。 我正在尝试为图形程序 (G'MIC) 写出命令列表,并使用 Python 3.6.4 (OS: Windows 10 Pro),我需要写很多行代码,但每一行都必须有不同的数字集(整数和浮点数).. 例如,在我现在尝试使用的代码中,语法是这样的:
gmic v -99 input_.png fx_pastell
0.5,1,0,10,40,633,11,95,1,1,0.291651967558584,0,0,8,150,1,81,1,0,0,0 -o
out_001.png
^ 所有变量都表示特定脚本中的参数(在本例中为 Pastell 效果)。
我编写的代码依赖于 Random 模块为我想要更改的每个参数获取一定范围内的特定数字。 我的绊脚石是让脚本在每次打印一行时输出不同的随机数。这是我的代码(这很糟糕,我知道..):
import random
a = random.randint (3,13)
b = random.randint (1,68)
c = random.randint (1,682)
d = random.randint (2,12)
e = random.randint (1,109)
g = random.randint (1,8)
h = random.uniform (0, 1)
k = random.randint (1,11)
l = random.randint (1,201)
n = random.randint (1,300)
o = random.randint (1,4)
dataFile = open("gmic1.txt", "w")
for line in range(120):
dataFile.write("gmic v -99 input_.png fx_pastell 0.5,1,0" + "," + str(a)
+ "," + str(b) + "," + str(c) + "," +str(d) + "," + str(e) + ",1," + str(g)
+ "," + str(h) + ",0,0," + str(k) + "," + str(l) + ",1," + str(n) + "," +
str(o) +"," + "0,0,0 -o out_%04d.png \n" % line)
dataFile.close()
输出是这样的:
gmic v -99 input_.png fx_pastell
0.5,1,0,12,2,521,12,85,1,7,0.04003331068764937,0,0,8,17,1,297,2,0,0,0 -o
out_0000.png
gmic v -99 input_.png fx_pastell
0.5,1,0,12,2,521,12,85,1,7,0.04003331068764937,0,0,8,17,1,297,2,0,0,0 -o
out_0001.png
.. etc...
没有这样的错误,但没有像我希望的那样每次都输出一组不同的数字。
我可以用 Notepad++ 的列编辑器更改第一个输入图像名称变量,这样就可以解决这个问题,我只需要知道如何使每一行代码不同。
非常感谢任何帮助!
你应该在每次迭代时调用 rand 函数
from random import randint, uniform
dataFile = open("gmic1.txt", "w")
for line in range(120):
dataFile.write("gmic v -99 input_.png fx_pastell 0.5,1,0"
+ "," + str(randint(3, 13))
+ "," + str(randint(1, 68))
+ "," + str(randint(1, 682))
+ "," + str(randint(2, 12))
+ "," + str(randint(1, 109))
+ ",1," + str(randint(1, 8))
+ "," + str(uniform(0, 1))
+ ",0,0," + str(randint(1, 11))
+ "," + str(randint(1, 201))
+ ",1," + str(randint(1, 300))
+ "," + str(randint(1, 4))
+ "," + "0,0,0 -o out_%04d.png \n" % line)
dataFile.close()