cipheron

Lottery ticket calculator

Mar 2nd, 2026
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. import random
  2. from math import comb
  3.  
  4. # 6 from 49 lottery
  5. BALLS = 49
  6. PICK = 6
  7. TOTAL = comb(BALLS, PICK) # 13,983,816
  8.  
  9. def combo_to_index(combo):
  10. idx = 0
  11. for i, val in enumerate(combo):
  12. idx += comb(val, i + 1)
  13. return idx
  14.  
  15. # Bitfield: ceil(TOTAL / 8) bytes ~ about 1.75 MB total
  16. bitfield = bytearray((TOTAL + 7) // 8)
  17. MB = len(bitfield) / (1024 * 1024)
  18.  
  19. print(f"Total possible tickets: {TOTAL:,}")
  20. print(f"Bitfield size: {len(bitfield):,} bytes ({MB:.2f} MB)")
  21. print(f"Simulating random purchases until 99% coverage...")
  22. print(f"{'Tickets Bought':>15} | {'Unique Found':>14} | {'Coverage':>10}")
  23. print("-" * 47)
  24.  
  25. unique_found = 0
  26. tickets_bought = 0
  27. target_unique = int(0.99 * TOTAL)
  28.  
  29. while unique_found < target_unique:
  30. # sample 0-indexed so combinatorial ranking works cleanly
  31. ticket = tuple(sorted(random.sample(range(BALLS), PICK)))
  32. tickets_bought += 1
  33.  
  34. idx = combo_to_index(ticket)
  35. byte_pos = idx >> 3 # idx // 8
  36. bit_pos = idx & 7 # idx % 8
  37. mask = 1 << bit_pos
  38.  
  39. if not (bitfield[byte_pos] & mask):
  40. bitfield[byte_pos] |= mask
  41. unique_found += 1
  42.  
  43. if tickets_bought % 1000000 == 0: # only printout every million tickets sold
  44. coverage = unique_found / TOTAL
  45. print(f"{tickets_bought:>15,} | {unique_found:>14,} | {coverage:>9.4%}")
  46.  
  47. # Final result
  48. coverage = unique_found / TOTAL
  49. print("-" * 47)
  50. print(f"{tickets_bought:>15,} | {unique_found:>14,} | {coverage:>9.4%} <- 99% reached!")
  51. print(f"\nTickets bought vs total combos: {tickets_bought / TOTAL:.2f}x")
  52. input()
Advertisement
Add Comment
Please, Sign In to add comment