2

I am trying to generate random numbers in Python 2.7 on 64-bit Windows system through the following line of code:

random_state=numpy_rng.random_integers(1e10)

But I am getting the following error.

OverflowError: Python int too large to convert to C long.

with the following trace back rand_num_generator = numpy.random.RandomState(random_state) File "mtrand.pyx", line 618, in mtrand.RandomState.init (numpy\random\mtrand\mtrand.c:8275) File "mtrand.pyx", line 654, in mtrand.RandomState.seed (numpy\random\mtrand\mtrand.c:8670) ValueError: Seed must be between 0 and 4294967295

3
  • I am not sure if you already know this but it is not the random integer that can't be too large, it is the seed. Is there any specific reason you are using that large of a seed? Commented May 27, 2016 at 7:08
  • Sorry yes you are right its a seed. Actually its a a code from the net that i am trying to run on my system . Does seed do anything else other than setting the starting value of the random number? Commented May 27, 2016 at 7:32
  • No, just the initial value. You can use another number, it doesn't have to be that big. Commented May 27, 2016 at 8:20

2 Answers 2

2

Max integer in your Python is:

import sys

sys.maxint
Out[61]: 2147483647

Or appr. 2.1e9. That's limitation of Windows. From this post:

In their infinitive wisdom Microsoft has decided to make the 'long' C type always a 32 bit signed integer - even on 64bit systems.

So, you can't use random_integers with arguments more than that number. You can use instead this trick:

10 * np.random.random_integers(1e9) - np.random.choice(10)
Out[62]: 3910179327L

Approach of @2Cubed with randint(0, 1e10) also should work, cause through randint python successfully convert int to long.

Sign up to request clarification or add additional context in comments.

5 Comments

@ Vadim . thanx for the reply. But sorry i couldn't understand the trick because the range is still not 1e10
The code was Ok, it gives integer in range(1, 1e10+1).
I rollbacked the code. And what is the error? OverflowError or ValueError: Seed must be between 0 and 4294967295.
Following is the complete traceback rand_num_generator = numpy.random.RandomState(random_state) File "mtrand.pyx", line 618, in mtrand.RandomState.__init__ (numpy\random\mtrand\mtrand.c:8275) File "mtrand.pyx", line 654, in mtrand.RandomState.seed (numpy\random\mtrand\mtrand.c:8670) ValueError: Seed must be between 0 and 4294967295
You should use seed for obtaining repeatable result. If you use seed > 4294967295, that means you want to have more than 4294967296 repeatable sequences. If so, it's better to post another question like "how to get quantity of repeatable results in range more than numpy.seed lets". Otherwise, recheck your code. Maybe you don't need seed at all?
1

The following should work, using NumPy.

from numpy.random import randint

randint(1e10)
# 6073545190

You may also use the built-in random.randint to accomplish the same task, with differences between this and the numpy.random.randint function described here.

from random import randint

randint(0, 1e10)
# 7978154001

4 Comments

I tried the above mentioned code but still i am getting the same error
I tried the above mentioned code but still i am getting the same.For the 2nd line of code it has following error errorValueError: Seed must be between 0 and 4294967295
@ATIF: Strange. Could you update your question with the entire stack trace, please?
Yes sure i have added the entire stack trace

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.