Choose a random item from range(stop) or range(start, stop[, step]). Roughly equivalent to ``choice(range(start, stop, step))`` but supports arbitrarily large ranges and is optimized for common cases.
(self, start, stop=None, step=_ONE)
| 282 | ## -------------------- integer methods ------------------- |
| 283 | |
| 284 | def randrange(self, start, stop=None, step=_ONE): |
| 285 | """Choose a random item from range(stop) or range(start, stop[, step]). |
| 286 | |
| 287 | Roughly equivalent to ``choice(range(start, stop, step))`` but |
| 288 | supports arbitrarily large ranges and is optimized for common cases. |
| 289 | |
| 290 | """ |
| 291 | |
| 292 | # This code is a bit messy to make it fast for the |
| 293 | # common case while still doing adequate error checking. |
| 294 | try: |
| 295 | istart = _index(start) |
| 296 | except TypeError: |
| 297 | istart = int(start) |
| 298 | if istart != start: |
| 299 | _warn('randrange() will raise TypeError in the future', |
| 300 | DeprecationWarning, 2) |
| 301 | raise ValueError("non-integer arg 1 for randrange()") |
| 302 | _warn('non-integer arguments to randrange() have been deprecated ' |
| 303 | 'since Python 3.10 and will be removed in a subsequent ' |
| 304 | 'version', |
| 305 | DeprecationWarning, 2) |
| 306 | if stop is None: |
| 307 | # We don't check for "step != 1" because it hasn't been |
| 308 | # type checked and converted to an integer yet. |
| 309 | if step is not _ONE: |
| 310 | raise TypeError('Missing a non-None stop argument') |
| 311 | if istart > 0: |
| 312 | return self._randbelow(istart) |
| 313 | raise ValueError("empty range for randrange()") |
| 314 | |
| 315 | # stop argument supplied. |
| 316 | try: |
| 317 | istop = _index(stop) |
| 318 | except TypeError: |
| 319 | istop = int(stop) |
| 320 | if istop != stop: |
| 321 | _warn('randrange() will raise TypeError in the future', |
| 322 | DeprecationWarning, 2) |
| 323 | raise ValueError("non-integer stop for randrange()") |
| 324 | _warn('non-integer arguments to randrange() have been deprecated ' |
| 325 | 'since Python 3.10 and will be removed in a subsequent ' |
| 326 | 'version', |
| 327 | DeprecationWarning, 2) |
| 328 | width = istop - istart |
| 329 | try: |
| 330 | istep = _index(step) |
| 331 | except TypeError: |
| 332 | istep = int(step) |
| 333 | if istep != step: |
| 334 | _warn('randrange() will raise TypeError in the future', |
| 335 | DeprecationWarning, 2) |
| 336 | raise ValueError("non-integer step for randrange()") |
| 337 | _warn('non-integer arguments to randrange() have been deprecated ' |
| 338 | 'since Python 3.10 and will be removed in a subsequent ' |
| 339 | 'version', |
| 340 | DeprecationWarning, 2) |
| 341 | # Fast path. |
no test coverage detected