As @chepner mentioned there is only one module random to patch it is wrong, maybe you can update your code like below that way your issue should be resolved. let me know if issue still seen
modul1.py:
from random import random
def function():
return random()
modul2.py:
from random import random
def function():
return random()
The unit_test:
def test_function():
with mock.patch('modul1.random', return_value=1), \
mock.patch('modul2.random', return_value=0):
val1 = modul1.function()
val2 = modul2.function()
assert not val1 == val2