Conditional Even Probability
Probability of generating number depends on the previous number
Conditional Even Probability
Problem Statement
Generate a sequence of single-byte values (8-bit unsigned integers) where the probability of generating an even or odd number depends on the previous number:
- If the previous value was odd: Next value is even with 75% probability, odd with 25% probability
- If the previous value was even: Next value is even with 25% probability, odd with 75% probability
This demonstrates conditional weighted randomization based on prior state.
Constraints
- Value range: 0-255 (8-bit)
- Conditional probability distribution based on parity of previous value
- Pattern: Odd values tend to be followed by even values (75% chance), and even values tend to be followed by odd values (75% chance)
- Creates a "bouncing" pattern between even and odd numbers
Examples
Example 1:
Input: Start with value 5 (odd)
Output: Next value should be even with 75% probability
Possible valid sequences (75% of the time):
- [5, 10, ...] where 10 is even
- [5, 20, ...] where 20 is even
- [5, 100, ...] where 100 is even
Possible valid sequences (25% of the time):
- [5, 11, ...] where 11 is odd
- [5, 21, ...] where 21 is oddExample 2:
Input: Start with value 8 (even)
Output: Next value should be odd with 75% probability
Likely continuation:
- [8, 3, ...] where 3 is odd (75% chance)
- [8, 99, ...] where 99 is odd (75% chance)
Unlikely continuation (25% chance):
- [8, 10, ...] where 10 is even (25% chance)Example 3:
Input: Generate sequence starting from value 1 (odd)
Sequence: [1, 100, 55, 200, 127, ...]
Analysis:
- After 1 (odd): 100 (even) - follows 75% probability rule ✓
- After 100 (even): 55 (odd) - follows 75% probability rule ✓
- After 55 (odd): 200 (even) - follows 75% probability rule ✓
- After 200 (even): 127 (odd) - follows 75% probability rule ✓
Expected pattern: alternating even-odd with weights as specifiedExample 4:
Input: Validate [2, 5, 8, 3, 6, 1, 4, ...]
Trace:
- Start: 2 (even) → next should be odd (75%) or even (25%): next is 5 (odd) ✓
- prev=5 (odd) → next should be even (75%) or odd (25%): next is 8 (even) ✓
- prev=8 (even) → next should be odd (75%) or even (25%): next is 3 (odd) ✓
- prev=3 (odd) → next should be even (75%) or odd (25%): next is 6 (even) ✓
Output: Valid sequence following the conditional probability rulesLoading the interactive workspace...