what exactly is the difference between these three functions (urandom-module, uos.random, os.random) to create random numbers?
I am using an ESP8266 and I have implemented some functions to create some random keys of a specific length:
Code: Select all
key_size_bits = 4096
#urandom-module:
def urandomnumb(ksb = key_size_bits):
import urandom
by_array = bytearray()
i = 0
end_while = int(ksb/8)
while i < end_while: #maybe the best solution to save RAM-Size, compare with i in range()?
by_array.append(urandom.getrandbits(8))
i += 1
return int.from_bytes(by_array,'big')
#uos.urandom:
def uosurandnumb(ksb = key_size_bits):
from uos import urandom
randnumb = urandom(int(ksb/8))
return int.from_bytes(randnumb,'big')
#os.urandom:
def osurandnumber(ksb = key_size_bits):
from os import urandom
randnumb = urandom(int(ksb/8))
return int.from_bytes(randnumb,'big')
- urandomnumb (urandom-module): between 58k - 61k microseconds
- uosurandnumb (uos.urandom): between 600 - 800 microseconds* *'*
- osurandnumber (os.urandom): between 4.7k - 5k microseconds* *''*
For the urandomnumb()-function, this is because of the while-loop and the process to create the number -- urandom.getrandbits() is limited to 32 bits, so I implemented a special way to generate a longer key, which will be similar to the uos.urandom and os.random-method?
But why did os.urandom need more time to calculate than uos.random? Are both the same - is it better to use uos.random, because it seems to be faster?
And what exactly is the difference between these three functions (urandom-module, uos.random, os.random) to create random numbers?
BTW:
*Every seventh call as a duration of *'*~3000 microseconds (uosurandnumb) and *''*7000 microseconds (osurandnumber) - these are very unusual peaks? Any explanation for this?