Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- from math import comb
- # 6 from 49 lottery
- BALLS = 49
- PICK = 6
- TOTAL = comb(BALLS, PICK) # 13,983,816
- def combo_to_index(combo):
- idx = 0
- for i, val in enumerate(combo):
- idx += comb(val, i + 1)
- return idx
- # Bitfield: ceil(TOTAL / 8) bytes ~ about 1.75 MB total
- bitfield = bytearray((TOTAL + 7) // 8)
- MB = len(bitfield) / (1024 * 1024)
- print(f"Total possible tickets: {TOTAL:,}")
- print(f"Bitfield size: {len(bitfield):,} bytes ({MB:.2f} MB)")
- print(f"Simulating random purchases until 99% coverage...")
- print(f"{'Tickets Bought':>15} | {'Unique Found':>14} | {'Coverage':>10}")
- print("-" * 47)
- unique_found = 0
- tickets_bought = 0
- target_unique = int(0.99 * TOTAL)
- while unique_found < target_unique:
- # sample 0-indexed so combinatorial ranking works cleanly
- ticket = tuple(sorted(random.sample(range(BALLS), PICK)))
- tickets_bought += 1
- idx = combo_to_index(ticket)
- byte_pos = idx >> 3 # idx // 8
- bit_pos = idx & 7 # idx % 8
- mask = 1 << bit_pos
- if not (bitfield[byte_pos] & mask):
- bitfield[byte_pos] |= mask
- unique_found += 1
- if tickets_bought % 1000000 == 0: # only printout every million tickets sold
- coverage = unique_found / TOTAL
- print(f"{tickets_bought:>15,} | {unique_found:>14,} | {coverage:>9.4%}")
- # Final result
- coverage = unique_found / TOTAL
- print("-" * 47)
- print(f"{tickets_bought:>15,} | {unique_found:>14,} | {coverage:>9.4%} <- 99% reached!")
- print(f"\nTickets bought vs total combos: {tickets_bought / TOTAL:.2f}x")
- input()
Advertisement
Add Comment
Please, Sign In to add comment