Is there a bug in Python 3 random.SystemRandom.randint, or am I using it incorrectly?

>>> import random >>> random.SystemRandom.randint(0, 10) Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> random.SystemRandom.randint(0, 10) TypeError: randint() missing 1 required positional argument: 'b' 

SystemRandom should give random numbers, although os.urandom , randint works like a normal randrange :

 >>> print(random.SystemRandom.randint.__doc__) Return random integer in range [a, b], including both end points. 

A small pop-up sentence appears in IDLE when I type it saying

 `random.SystemRandom.randint(self, a, b)` 

I think that is the reason. I don't use classes very well and understand how they work, but the first argument seems to be passed as self when it should be a . I never understand why self used when it is not even a keyword, and how it should work, but it usually does.

Am I doing this wrong, or should I report it to the Python Foundation when someone should do such things?

+7
python random
source share
2 answers

random.SystemRandom is a class. It must be created,

 In [5]: foo = random.SystemRandom() In [6]: foo.randint(0, 10) Out[6]: 0 

A full docstring gives a hint:

 In [12]: random.SystemRandom.randint? Signature: random.SystemRandom.randint(self, a, b) Docstring: Return random integer in range [a, b], including both end points. File: /usr/local/lib/python3.4/random.py Type: function 

The self parameter indicates that this randint is a SystemRandom method.

+8
source share

I think you want:

 import random r = random.SystemRandom() print(r.randint(0, 10)) 

instead.

This is because you need to instantiate the class random.SystemRandom .

However, you can just as easily use the following:

 import random random.randint(0, 10) 

if you don’t need an OS dependent cryptographic RNG.

+7
source share

All Articles