MCPcopy Create free account
hub / github.com/apache/arrow / randdecimal

Function randdecimal

python/pyarrow/tests/util.py:75–109  ·  view source on GitHub ↗

Generate a random decimal value with specified precision and scale. Parameters ---------- precision : int The maximum number of digits to generate. Must be an integer between 1 and 38 inclusive. scale : int The maximum number of digits following the decimal p

(precision, scale)

Source from the content-addressed store, hash-verified

73
74
75def randdecimal(precision, scale):
76 """Generate a random decimal value with specified precision and scale.
77
78 Parameters
79 ----------
80 precision : int
81 The maximum number of digits to generate. Must be an integer between 1
82 and 38 inclusive.
83 scale : int
84 The maximum number of digits following the decimal point. Must be an
85 integer greater than or equal to 0.
86
87 Returns
88 -------
89 decimal_value : decimal.Decimal
90 A random decimal.Decimal object with the specified precision and scale.
91 """
92 assert 1 <= precision <= 38, 'precision must be between 1 and 38 inclusive'
93 if scale < 0:
94 raise ValueError(
95 'randdecimal does not yet support generating decimals with '
96 'negative scale'
97 )
98 max_whole_value = 10 ** (precision - scale) - 1
99 whole = random.randint(-max_whole_value, max_whole_value)
100
101 if not scale:
102 return decimal.Decimal(whole)
103
104 max_fractional_value = 10 ** scale - 1
105 fractional = random.randint(0, max_fractional_value)
106
107 return decimal.Decimal(
108 f'{whole}.{str(fractional).rjust(scale, "0")}'
109 )
110
111
112def random_ascii(length):

Callers

nothing calls this directly

Calls 1

DecimalMethod · 0.45

Tested by

no test coverage detected