diff --git a/faker/providers/__init__.py b/faker/providers/__init__.py index 9bb9f47b1f..b31d2868bf 100644 --- a/faker/providers/__init__.py +++ b/faker/providers/__init__.py @@ -151,10 +151,15 @@ def random_number(self, digits=None, fix_len=False): fixed length number """ if digits is None: - digits = self.random_digit() + digits = self.random_digit_not_null() + if digits < 0: + raise ValueError("The digit parameter must be greater than or equal to 0.") if fix_len: - return self.generator.random.randint( - pow(10, digits - 1), pow(10, digits) - 1) + if digits > 0: + return self.generator.random.randint( + pow(10, digits - 1), pow(10, digits) - 1) + else: + raise ValueError("A number of fixed length cannot have less than 1 digit in it.") else: return self.generator.random.randint(0, pow(10, digits) - 1) diff --git a/tests/test_factory.py b/tests/test_factory.py index 92c09cc754..cbea6a8a0c 100644 --- a/tests/test_factory.py +++ b/tests/test_factory.py @@ -912,6 +912,14 @@ def test_random_number(self): number = provider.random_number(10, True) assert len(str(number)) == 10 + # Digits parameter < 0 + with self.assertRaises(ValueError): + number = provider.random_number(-1, True) + + # Digits parameter < 1 with fix_len=True + with self.assertRaises(ValueError): + number = provider.random_number(0, True) + def test_instance_seed_chain(self): factory = Factory.create()