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

ATIF
  • 21
  • 4
  • 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? – ayhan May 27 '16 at 07: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? – ATIF May 27 '16 at 07:32
  • No, just the initial value. You can use another number, it doesn't have to be that big. – ayhan May 27 '16 at 08:20

2 Answers2

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.

Community
  • 1
  • 1
Vadim Shkaberda
  • 2,807
  • 19
  • 35
  • @ Vadim . thanx for the reply. But sorry i couldn't understand the trick because the range is still not 1e10 – ATIF May 27 '16 at 07:12
  • The code was Ok, it gives integer in `range(1, 1e10+1)`. – Vadim Shkaberda May 27 '16 at 07:46
  • I rollbacked the code. And what is the error? `OverflowError` or `ValueError: Seed must be between 0 and 4294967295`. – Vadim Shkaberda May 27 '16 at 07:49
  • 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 – ATIF May 27 '16 at 07:55
  • 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? – Vadim Shkaberda May 27 '16 at 08:08
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
Community
  • 1
  • 1
2Cubed
  • 3,401
  • 7
  • 23
  • 40
  • I tried the above mentioned code but still i am getting the same error – ATIF May 27 '16 at 03:01
  • 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 May 27 '16 at 03:12
  • @ATIF: Strange. Could you update your question with the entire stack trace, please? – 2Cubed May 27 '16 at 05:11
  • Yes sure i have added the entire stack trace – ATIF May 27 '16 at 07:10