Skip to content

Commit 049fa39

Browse files
authored
use better random seeds and tell others about it (#1340)
* use better random seeds and tell others about it * add more details about rng reasoning
1 parent 247ad2a commit 049fa39

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

examples/anonymize_dataset.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@
194194
# To ensure results are reproducible across runs, you can pass the
195195
# ``random_state`` parameter, causing the random number generator to produce
196196
# the same results every time you execute the function. This may come in handy
197-
# e.g. in situations where you discover a problem with the data while working
197+
# in situations where you discover a problem with the data while working
198198
# with the anonymized dataset, fix the issue in the original dataset, and
199199
# run anonymization again.
200200
#
@@ -203,6 +203,15 @@
203203
# in a dataset with multiple subjects will the effects of randomly-picked IDs
204204
# become apparent.)
205205
#
206+
# A good random seed is truly random. Avoid using random seeds from popular
207+
# culture, like "42", or "1337". To obtain a truly random seed, you can paste
208+
# the following into your console:
209+
# ``python -c "import secrets; print(secrets.randbits(31))"``
210+
# Here, 31 bits correspond to the maximum seed "size" that the the legacy
211+
# ``RandomState`` by NumPy, which many scientific libraries still rely on,
212+
# can accept. For more information, see also this blog post on
213+
# `NumPy RNG best practices <https://blog.scientific-python.org/numpy/numpy-rng/>`_.
214+
#
206215
# .. note::
207216
# Passing ``random_state`` merely guarantees that subject IDs and time shift
208217
# remain the same across anonymization runs if the original dataset
@@ -218,6 +227,6 @@
218227
bids_root_in=bids_root,
219228
bids_root_out=bids_root_anon,
220229
datatypes="meg",
221-
random_state=42,
230+
random_state=293201004,
222231
)
223232
print_dir_tree(bids_root_anon)

mne_bids/tests/test_write.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3868,7 +3868,7 @@ def test_anonymize_dataset(_bids_validate, tmpdir):
38683868
bids_root_in=bids_root,
38693869
bids_root_out=bids_root_anon,
38703870
datatypes=["meg", "anat"],
3871-
random_state=42,
3871+
random_state=1442792182,
38723872
)
38733873
_bids_validate(bids_root_anon)
38743874
assert (bids_root_anon / "sub-1" / "ses-01" / "meg").exists()

0 commit comments

Comments
 (0)