功能在for循环

功能在for循环

问题描述:

这是我的代码:功能在for循环

import random 

a = 10000 
b = random.random() 
c = 2 

def random1(a,b,c): 
    z = a * b * c 
    print(z) 

def random2(): 
    for i in range(10): 
     random1(a,b,c) 

random2() 

我有我的输出问题,因为功能random2()给了我十个一模一样的数字,例如:

10652.014111193188 
10652.014111193188 
10652.014111193188 
10652.014111193188 
10652.014111193188 
10652.014111193188 
10652.014111193188 
10652.014111193188 
10652.014111193188 
10652.014111193188 

错误在哪里?或者我不能使用在for循环中给我随机数的函数:/。一切都很好,当我循环公式而不是功能时,问题只显示功能在for循环。

+1

中没有任何'random1'或'random2'随机的。这里唯一的RNG调用是'b = random.random()',它获得**一个随机数**并将其分配给'b'。它不会使“b”简写为“call'random.random” – user2357112

+2

或换句话说,“我掷了一次骰子,看了10次,每次都看到相同的数字!是什么给了?“ – user2357112

+0

尝试'b = random.random',然后称它为'random1(a,b(),c)'。 –

一个简单的改变应该这样做,结合brandom.random,然后调用b()同时传递到random1

import random 

a = 10000 
b = random.random 
c = 2 

def random1(a,b,c): 
    z = a * b * c 
    print(z) 

def random2(): 
    for i in range(10): 
     # call b() here instead of b 
     random1(a,b(),c) 

random2() 
+0

很好,这对我很有用。 –

b永远不会改变,即使您使用随机值初始化它。 ac都不会发生变化,但您可能已经预料到了这一点。

其实你不应该把B插入随机函数在所有!如果你想要的功能Random1创建乘以A和C的随机数:

进口随机

a = 10000 
# remarked: b = random.random() 
c = 2 

def random1(a,c): 
    b = random.random() 
    z = a * b * c 
    print(z) 



def random2(): # I would call it testRandom1 
    for i in range(10): 
     random1(a ,c) 

random2() # test random1 ten times.