The problem is the scope of the num
variable.
It is declared inside the if statement:
if semente != None:
num=semente
And you're trying to access it in the scope of the function:
num_aleatorio = (num*a+b)%m
It will give error because for the scope of the function fnum_aleatorio
the variable num
does not exist. It only exists within the scope of if
.
One way to fix this is to do:
num = semente if semente is not None else 1
.
Where 1
is the default value of num
when semente
is None
.
In this way you will be declaring the variable num
within the scope of the function fnum_aleatorio
.
So, I would stay:
def fnum_aleatorio(a, b, m, semente=None):
num = semente if semente is not None else 1
num_aleatorio = (num*a+b)%m
if num_aleatorio <= a//2:
return 0
else:
return 1