Issue #24620: Random.setstate() now validates the value of state last element.
diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c
index 4377ee0..416e266 100644
--- a/Modules/_randommodule.c
+++ b/Modules/_randommodule.c
@@ -340,6 +340,10 @@
     index = PyLong_AsLong(PyTuple_GET_ITEM(state, i));
     if (index == -1 && PyErr_Occurred())
         return NULL;
+    if (index < 0 || index > N) {
+        PyErr_SetString(PyExc_ValueError, "invalid state");
+        return NULL;
+    }
     self->index = (int)index;
 
     Py_INCREF(Py_None);