0 | |
From 2958f6327ad3a5222eb773801e3e46f6130d9f65 Mon Sep 17 00:00:00 2001
|
1 | |
From: "Benjamin A. Beasley" <code@musicinmybrain.net>
|
2 | |
Date: Tue, 14 Jun 2022 11:27:05 -0400
|
3 | |
Subject: [PATCH] Preserve existing Random seed behavior in Python 3.11
|
4 | |
|
5 | |
Fixes #602. See
|
6 | |
https://github.com/holoviz/param/issues/602#issuecomment-1155147795 for
|
7 | |
rationale and caveats.
|
8 | |
---
|
9 | |
numbergen/__init__.py | 13 ++++++++++++-
|
10 | |
1 file changed, 12 insertions(+), 1 deletion(-)
|
11 | |
|
12 | |
diff --git a/numbergen/__init__.py b/numbergen/__init__.py
|
13 | |
index 2faf2bcb..d8ad7e1b 100644
|
14 | |
--- a/numbergen/__init__.py
|
15 | |
+++ b/numbergen/__init__.py
|
16 | |
@@ -8,6 +8,7 @@
|
17 | |
import struct
|
18 | |
import fractions
|
19 | |
|
20 | |
+from ctypes import c_size_t
|
21 | |
from math import e,pi
|
22 | |
|
23 | |
import param
|
24 | |
@@ -306,8 +307,18 @@ class TimeAwareRandomState(TimeAware):
|
25 | |
explicitly when you construct the RandomDistribution object.
|
26 | |
"""
|
27 | |
|
28 | |
+ # Historically, the default random state was seeded with the tuple
|
29 | |
+ # (500, 500). The CPython implementation implicitly formed an unsigned
|
30 | |
+ # integer seed using the hash of the tuple as in the expression below. Note
|
31 | |
+ # that the resulting integer, and therefore the default initial random
|
32 | |
+ # state, varies across CPython versions (as the hash algorithm has changed)
|
33 | |
+ # and also between 32-bit and 64-bit interpreters.
|
34 | |
+ #
|
35 | |
+ # Seeding based on hashing is deprecated since Python 3.9 and removed in
|
36 | |
+ # Python 3.11; we explicitly continue the historical behavior for the time
|
37 | |
+ # being.
|
38 | |
random_generator = param.Parameter(
|
39 | |
- default=random.Random((500,500)), doc=
|
40 | |
+ default=random.Random(c_size_t(hash((500,500))).value), doc=
|
41 | |
"""
|
42 | |
Random state used by the object. This may may be an instance
|
43 | |
of random.Random from the Python standard library or an
|