mirror of
https://github.com/wassname/ray.git
synced 2026-07-20 12:40:20 +08:00
Make sure user seeding does not affect actor ID generation. (#506)
* Make sure user seeding does not affect actor ID generation. * Fix linting. * Add test.
This commit is contained in:
committed by
Philipp Moritz
parent
e50a23b820
commit
245c8ab888
@@ -3,11 +3,37 @@ from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import binascii
|
||||
import numpy as np
|
||||
import sys
|
||||
|
||||
import ray.local_scheduler
|
||||
|
||||
|
||||
def random_string():
|
||||
"""Generate a random string to use as an ID.
|
||||
|
||||
Note that users may seed numpy, which could cause this function to generate
|
||||
duplicate IDs. Therefore, we need to seed numpy ourselves, but we can't
|
||||
interfere with the state of the user's random number generator, so we extract
|
||||
the state of the random number generator and reset it after we are done.
|
||||
|
||||
TODO(rkn): If we want to later guarantee that these are generated in a
|
||||
deterministic manner, then we will need to make some changes here.
|
||||
|
||||
Returns:
|
||||
A random byte string of length 20.
|
||||
"""
|
||||
# Get the state of the numpy random number generator.
|
||||
numpy_state = np.random.get_state()
|
||||
# Try to use true randomness.
|
||||
np.random.seed(None)
|
||||
# Generate the random ID.
|
||||
random_id = np.random.bytes(20)
|
||||
# Reset the state of the numpy random number generator.
|
||||
np.random.set_state(numpy_state)
|
||||
return random_id
|
||||
|
||||
|
||||
def decode(byte_str):
|
||||
"""Make this unicode in Python 3, otherwise leave it as bytes."""
|
||||
if sys.version_info >= (3, 0):
|
||||
|
||||
Reference in New Issue
Block a user