Returns a pseudorandom value modulo ``modulus`` based on the input ``value`` and attempt-specific ``step`` size. >>> rand_fn(0, 0, 0) Traceback (most recent call last): ... ZeroDivisionError: integer division or modulo by zero >>> rand_fn
(value: int, step: int, modulus: int)
| 56 | # To make retries easier, we will instead use ``f(x) = (x**2 + C) % num`` |
| 57 | # where ``C`` is a value that we can modify between each attempt. |
| 58 | def rand_fn(value: int, step: int, modulus: int) -> int: |
| 59 | """ |
| 60 | Returns a pseudorandom value modulo ``modulus`` based on the |
| 61 | input ``value`` and attempt-specific ``step`` size. |
| 62 | |
| 63 | >>> rand_fn(0, 0, 0) |
| 64 | Traceback (most recent call last): |
| 65 | ... |
| 66 | ZeroDivisionError: integer division or modulo by zero |
| 67 | >>> rand_fn(1, 2, 3) |
| 68 | 0 |
| 69 | >>> rand_fn(0, 10, 7) |
| 70 | 3 |
| 71 | >>> rand_fn(1234, 1, 17) |
| 72 | 16 |
| 73 | """ |
| 74 | return (pow(value, 2) + step) % modulus |
| 75 | |
| 76 | for _ in range(attempts): |
| 77 | # These track the position within the cycle detection logic. |