Python Glossary Русский

Python 3.6+Linuxosfunction

os.getrandom

os — randomness

Return random bytes directly from the system getrandom source. Availability: Linux.

Syntax

os.getrandom(...)

Application code is better served by secrets or os.urandom: they are portable and need no flags. The function may block early in boot until the generator has gathered entropy; the GRND_NONBLOCK flag turns the wait into BlockingIOError. The random module is unsuitable for passwords and tokens — it is predictable.

Examples

import os
print(len(os.getrandom(16)))   # → ? только Linux: 16 (на Windows/macOS функции нет — переносимый аналог os.urandom)
key = os.getrandom(32, os.GRND_NONBLOCK)   # → ? только Linux: BlockingIOError, если энтропия ещё не набрана
print(len(key))   # → 32
print(len(os.getrandom(0)))   # → ? только Linux: 0, граничный случай — пустой запрос
print(os.getrandom(4) == os.getrandom(4))   # → ? только Linux: False, каждый вызов даёт новые байты

See also

Open in the full glossarydocs.python.orgReport an inaccuracy