最近因为某个项目需要生成0.01~0.1之间的随机数,正好在学习python,因此果断就用上了。。。
具体代码:
import random
out = []
f = open("outRandom.txt","w")
for i in range(0,512*384):
out.append(random.uniform(0.01,0.1))
out[i] = (out[i]-0.01)/(0.1-0.01)#量化到0~1
f.write(str(out[i]))
if (i+1)%512:
f.write('\t')
else:
f.write('\n')
f.close();
需要注意的是:
1.默认for循环是从0开始,即python与c/c++一样,数组下标是从0开始的;
2.python输出到文件时,只能是string类型,否则会报错,所以需要对数字进行强制转换;