Skip to content

fix: replace deprecated numpy.random.RandomState with Generator API#635

Merged
isuruperera merged 1 commit into
mainfrom
remediate-main-20260520-223503-2f175c9a
May 21, 2026
Merged

fix: replace deprecated numpy.random.RandomState with Generator API#635
isuruperera merged 1 commit into
mainfrom
remediate-main-20260520-223503-2f175c9a

Conversation

@sonarqube-agent
Copy link
Copy Markdown

This PR resolves 10 SonarQube S6711 issues in the hierarchical clustering tests by replacing all legacy numpy.random.RandomState calls with the modern numpy.random.default_rng API. This modernization improves code quality and aligns with current NumPy best practices for random number generation.

View Project in SonarCloud


Fixed Issues

python:S6711 - Use a "numpy.random.Generator" here instead of this legacy function. • MAJORView issue

Location: sklearn/cluster/tests/test_hierarchical.py:185

Why is this an issue?

Using a predictable seed is a common best practice when using NumPy to create reproducible results. To that end, using np.random.seed(number) to set the seed of the global numpy.random.RandomState has been the privileged solution for a long time.

What changed

Replaces the legacy np.random.RandomState(global_random_seed) call at line 185 with the modern np.random.default_rng(global_random_seed), switching from the deprecated RandomState to the recommended Generator API. This directly fixes the static analysis warning about using legacy numpy.random.RandomState at line 185.

--- a/sklearn/cluster/tests/test_hierarchical.py
+++ b/sklearn/cluster/tests/test_hierarchical.py
@@ -185,1 +185,1 @@ def test_agglomerative_clustering(global_random_seed, lil_container):
-    rng = np.random.RandomState(global_random_seed)
+    rng = np.random.default_rng(global_random_seed)
python:S6711 - Use a "numpy.random.Generator" here instead of this legacy function. • MAJORView issue

Location: sklearn/cluster/tests/test_hierarchical.py:277

Why is this an issue?

Using a predictable seed is a common best practice when using NumPy to create reproducible results. To that end, using np.random.seed(number) to set the seed of the global numpy.random.RandomState has been the privileged solution for a long time.

What changed

Replaces the legacy np.random.RandomState(global_random_seed) call at line 277 with the modern np.random.default_rng(global_random_seed). This directly fixes the static analysis warning about using legacy numpy.random.RandomState at line 277.

--- a/sklearn/cluster/tests/test_hierarchical.py
+++ b/sklearn/cluster/tests/test_hierarchical.py
@@ -277,1 +277,1 @@ def test_ward_agglomeration(global_random_seed):
-    rng = np.random.RandomState(global_random_seed)
+    rng = np.random.default_rng(global_random_seed)
python:S6711 - Use a "numpy.random.Generator" here instead of this legacy function. • MAJORView issue

Location: sklearn/cluster/tests/test_hierarchical.py:328

Why is this an issue?

Using a predictable seed is a common best practice when using NumPy to create reproducible results. To that end, using np.random.seed(number) to set the seed of the global numpy.random.RandomState has been the privileged solution for a long time.

What changed

Replaces the legacy np.random.RandomState(global_random_seed) call at line 328 with the modern np.random.default_rng(global_random_seed). This directly fixes the static analysis warning about using legacy numpy.random.RandomState at line 328.

--- a/sklearn/cluster/tests/test_hierarchical.py
+++ b/sklearn/cluster/tests/test_hierarchical.py
@@ -328,1 +328,1 @@ def test_sparse_scikit_vs_scipy(global_random_seed):
-    rng = np.random.RandomState(global_random_seed)
+    rng = np.random.default_rng(global_random_seed)
python:S6711 - Use a "numpy.random.Generator" here instead of this legacy function. • MAJORView issue

Location: sklearn/cluster/tests/test_hierarchical.py:367

Why is this an issue?

Using a predictable seed is a common best practice when using NumPy to create reproducible results. To that end, using np.random.seed(number) to set the seed of the global numpy.random.RandomState has been the privileged solution for a long time.

What changed

Replaces the legacy np.random.RandomState(global_random_seed) call at line 367 with the modern np.random.default_rng(global_random_seed). This directly fixes the static analysis warning about using legacy numpy.random.RandomState at line 367.

--- a/sklearn/cluster/tests/test_hierarchical.py
+++ b/sklearn/cluster/tests/test_hierarchical.py
@@ -367,1 +367,1 @@ def test_vector_scikit_single_vs_scipy_single(global_random_seed):
-    rng = np.random.RandomState(global_random_seed)
+    rng = np.random.default_rng(global_random_seed)
python:S6711 - Use a "numpy.random.Generator" here instead of this legacy function. • MAJORView issue

Location: sklearn/cluster/tests/test_hierarchical.py:466

Why is this an issue?

Using a predictable seed is a common best practice when using NumPy to create reproducible results. To that end, using np.random.seed(number) to set the seed of the global numpy.random.RandomState has been the privileged solution for a long time.

What changed

Replaces the legacy np.random.RandomState(global_random_seed) call at line 466 with the modern np.random.default_rng(global_random_seed). This directly fixes the static analysis warning about using legacy numpy.random.RandomState at line 466.

--- a/sklearn/cluster/tests/test_hierarchical.py
+++ b/sklearn/cluster/tests/test_hierarchical.py
@@ -466,1 +466,1 @@ def test_ward_tree_children_order(global_random_seed):
-    rng = np.random.RandomState(global_random_seed)
+    rng = np.random.default_rng(global_random_seed)
python:S6711 - Use a "numpy.random.Generator" here instead of this legacy function. • MAJORView issue

Location: sklearn/cluster/tests/test_hierarchical.py:270

Why is this an issue?

Using a predictable seed is a common best practice when using NumPy to create reproducible results. To that end, using np.random.seed(number) to set the seed of the global numpy.random.RandomState has been the privileged solution for a long time.

What changed

Replaces the legacy np.random.RandomState(0) call at line 270 with np.random.default_rng(0), and updates the associated rng.randn(50, 100) call to rng.standard_normal((50, 100)). This directly fixes the static analysis warning about using legacy numpy.random.RandomState at line 270.

--- a/sklearn/cluster/tests/test_hierarchical.py
+++ b/sklearn/cluster/tests/test_hierarchical.py
@@ -270,2 +270,2 @@ def test_agglomerative_clustering_memory_mapped():
-    rng = np.random.RandomState(0)
-    Xmm = create_memmap_backed_data(rng.randn(50, 100))
+    rng = np.random.default_rng(0)
+    Xmm = create_memmap_backed_data(rng.standard_normal((50, 100)))
python:S6711 - Use a "numpy.random.Generator" here instead of this legacy function. • MAJORView issue

Location: sklearn/cluster/tests/test_hierarchical.py:396

Why is this an issue?

Using a predictable seed is a common best practice when using NumPy to create reproducible results. To that end, using np.random.seed(number) to set the seed of the global numpy.random.RandomState has been the privileged solution for a long time.

What changed

Replaces the legacy np.random.RandomState(seed=1) call at line 396 with the modern np.random.default_rng(seed=1). This directly fixes the static analysis warning about using legacy numpy.random.RandomState at line 396.

--- a/sklearn/cluster/tests/test_hierarchical.py
+++ b/sklearn/cluster/tests/test_hierarchical.py
@@ -396,1 +396,1 @@ def test_mst_linkage_core_memory_mapped(metric_param_grid):
-    rng = np.random.RandomState(seed=1)
+    rng = np.random.default_rng(seed=1)
python:S6711 - Use a "numpy.random.Generator" here instead of this legacy function. • MAJORView issue

Location: sklearn/cluster/tests/test_hierarchical.py:157

Why is this an issue?

Using a predictable seed is a common best practice when using NumPy to create reproducible results. To that end, using np.random.seed(number) to set the seed of the global numpy.random.RandomState has been the privileged solution for a long time.

What changed

Replaces the legacy np.random.RandomState(0) call at line 157 with the modern np.random.default_rng(0), switching from the deprecated RandomState to the recommended Generator API. This directly fixes the static analysis warning about using legacy numpy.random.RandomState at line 157.

--- a/sklearn/cluster/tests/test_hierarchical.py
+++ b/sklearn/cluster/tests/test_hierarchical.py
@@ -157,1 +157,1 @@ def test_agglomerative_clustering_distances(
-    rng = np.random.RandomState(0)
+    rng = np.random.default_rng(0)
python:S6711 - Use a "numpy.random.Generator" here instead of this legacy function. • MAJORView issue

Location: sklearn/cluster/tests/test_hierarchical.py:104

Why is this an issue?

Using a predictable seed is a common best practice when using NumPy to create reproducible results. To that end, using np.random.seed(number) to set the seed of the global numpy.random.RandomState has been the privileged solution for a long time.

What changed

Replaces the legacy np.random.RandomState(0) call at line 104 with the modern np.random.default_rng(0), and updates the associated rng.randn(50, 100) call to rng.standard_normal((50, 100)) to use the new Generator API. This directly fixes the static analysis warning about using legacy numpy.random.RandomState at line 104.

--- a/sklearn/cluster/tests/test_hierarchical.py
+++ b/sklearn/cluster/tests/test_hierarchical.py
@@ -104,2 +104,2 @@ def test_unstructured_linkage_tree():
-    rng = np.random.RandomState(0)
-    X = rng.randn(50, 100)
+    rng = np.random.default_rng(0)
+    X = rng.standard_normal((50, 100))
python:S6711 - Use a "numpy.random.Generator" here instead of this legacy function. • MAJORView issue

Location: sklearn/cluster/tests/test_hierarchical.py:128

Why is this an issue?

Using a predictable seed is a common best practice when using NumPy to create reproducible results. To that end, using np.random.seed(number) to set the seed of the global numpy.random.RandomState has been the privileged solution for a long time.

What changed

Replaces the legacy np.random.RandomState(0) call at line 128 with the modern np.random.default_rng(0), switching from the deprecated RandomState to the recommended Generator API. This directly fixes the static analysis warning about using legacy numpy.random.RandomState at line 128.

--- a/sklearn/cluster/tests/test_hierarchical.py
+++ b/sklearn/cluster/tests/test_hierarchical.py
@@ -128,1 +128,1 @@ def test_height_linkage_tree():
-    rng = np.random.RandomState(0)
+    rng = np.random.default_rng(0)

Have a suggestion or found an issue? Share your feedback here.


SonarQube Remediation Agent uses AI. Check for mistakes.

Fixed issues:
- AZ45CwrgRXnEWm2Rf5JB for python:S6711 rule
- AZ45CwrgRXnEWm2Rf5I_ for python:S6711 rule
- AZ45CwrgRXnEWm2Rf5JH for python:S6711 rule
- AZ45CwrgRXnEWm2Rf5I8 for python:S6711 rule
- AZ45CwrgRXnEWm2Rf5I4 for python:S6711 rule
- AZ45CwrgRXnEWm2Rf5JE for python:S6711 rule
- AZ45CwrgRXnEWm2Rf5I6 for python:S6711 rule
- AZ45CwrgRXnEWm2Rf5JC for python:S6711 rule
- AZ45CwrgRXnEWm2Rf5I3 for python:S6711 rule
- AZ45CwrgRXnEWm2Rf5I1 for python:S6711 rule

Generated by SonarQube Agent (task: f53c07ae-503d-4c5d-90c2-ba487ef07b33)
@sonarqubecloud
Copy link
Copy Markdown

@isuruperera isuruperera merged commit cb6a68f into main May 21, 2026
11 checks passed
@isuruperera isuruperera deleted the remediate-main-20260520-223503-2f175c9a branch May 21, 2026 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants