Guest User

BA gacha pull system comparison with graph 3

a guest
Jul 28th, 2026
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.60 KB | Source Code | 0 0
  1. import random
  2. from collections import Counter
  3. import matplotlib.pyplot as plt
  4.  
  5. # ============================================================
  6. # Configuration
  7. # ============================================================
  8.  
  9. SIMULATIONS = 1_000_000
  10.  
  11. RATE_UP = 0.007  # 0.7%
  12. RATE_OFF = 0.00022115  # 0.022115%
  13.  
  14. STEP_SIZE = 1
  15.  
  16. GRAPH_LABEL_STEP_SIZE = 50
  17. EXTRA_GRAPH_LABEL_STEP = [70, 130, 150, 170, 270, 330, 350, 370]
  18.  
  19. NEW_SYSTEM_EXTRA_10_PULLS = [70, 130, 150, 170, 270, 330, 350, 370]
  20.  
  21.  
  22. # ============================================================
  23. # Gacha pull
  24. # ============================================================
  25.  
  26.  
  27. def pull(target):
  28.     """
  29.    Pull once.
  30.  
  31.    The currently targeted character has RATE_UP.
  32.    The other character has RATE_OFF.
  33.  
  34.    Returns:
  35.        "a"
  36.        "b"
  37.        None
  38.    """
  39.  
  40.     if target == "a":
  41.         rate_a = RATE_UP
  42.         rate_b = RATE_OFF
  43.     else:
  44.         rate_a = RATE_OFF
  45.         rate_b = RATE_UP
  46.  
  47.     roll = random.random()
  48.  
  49.     if roll < rate_a:
  50.         return "a"
  51.  
  52.     if roll < rate_a + rate_b:
  53.         return "b"
  54.  
  55.     return None
  56.  
  57.  
  58. # ============================================================
  59. # New rule pity
  60. # ============================================================
  61.  
  62.  
  63. def apply_new_rule_pity(pity_count):
  64.     """
  65.    Apply the new rule's pity system.
  66.  
  67.    100th pull:
  68.        50% chance to get the currently targeted character.
  69.  
  70.    200th pull:
  71.        Guaranteed currently targeted character.
  72.  
  73.    Returns:
  74.        True if pity gave the target.
  75.    """
  76.  
  77.     if pity_count == 100:
  78.         return random.random() < 0.5
  79.  
  80.     if pity_count == 200:
  81.         return True
  82.  
  83.     return None
  84.  
  85.  
  86. # ============================================================
  87. # Group 1 - Unlimited pulls old rule
  88. # ============================================================
  89.  
  90.  
  91. def simulate_group_unlimited_old_rule():
  92.     """
  93.    Unlimited pulls until both A and B are obtained.
  94.  
  95.    Rate-up starts on A.
  96.    Once A is obtained, rate-up switches to B.
  97.  
  98.    Returns:
  99.        Number of pulls required.
  100.    """
  101.  
  102.     have_a = False
  103.     have_b = False
  104.  
  105.     target = "a"
  106.  
  107.     pull_count = 0
  108.  
  109.     while not (have_a and have_b):
  110.         pull_count += 1
  111.  
  112.         result = pull(target)
  113.  
  114.         if result == "a":
  115.             have_a = True
  116.  
  117.             # Switch rate-up from A to B
  118.             if target == "a":
  119.                 target = "b"
  120.  
  121.         elif result == "b":
  122.             have_b = True
  123.  
  124.             # This normally means B appeared before A.
  125.             # Continue targeting A.
  126.             if target == "b":
  127.                 target = "a"
  128.  
  129.         # ----------------------------------------------------
  130.         # Old rule: choose missing character at 200
  131.         # ----------------------------------------------------
  132.  
  133.         if pull_count % 200 == 0:
  134.             if not have_a:
  135.                 have_a = True
  136.                 if target == "a":
  137.                     target = "b"
  138.             elif not have_b:
  139.                 have_b = True
  140.  
  141.     return pull_count
  142.  
  143.  
  144. # ============================================================
  145. # Group 1 - Unlimited pulls new rule
  146. # ============================================================
  147.  
  148.  
  149. def simulate_group_unlimited_new_rule():
  150.     """
  151.    Unlimited pulls until both A and B are obtained.
  152.  
  153.    Rate-up starts on A.
  154.    Once A is obtained, rate-up switches to B.
  155.    """
  156.  
  157.     have_a = False
  158.     have_b = False
  159.  
  160.     target = "a"
  161.     pull_count = 0
  162.     paid_pull_count = 0
  163.     pity_count = 0
  164.     free_pull_count = 0
  165.  
  166.     while not (have_a and have_b):
  167.         if free_pull_count == 0:
  168.             paid_pull_count += 1
  169.         else:
  170.             free_pull_count -= 1
  171.         pull_count += 1
  172.         pity_count += 1
  173.  
  174.         is_pity = apply_new_rule_pity(pity_count)
  175.         if is_pity:
  176.             if target == "a":
  177.                 have_a = True
  178.                 pity_count = 0
  179.                 target = "b"
  180.             elif target == "b":
  181.                 have_b = True
  182.         elif is_pity == False:
  183.             continue
  184.         if have_a and have_b:
  185.             break
  186.  
  187.         # extra pull from milestone
  188.         if pull_count in NEW_SYSTEM_EXTRA_10_PULLS:
  189.             free_pull_count += 10
  190.  
  191.         result = pull(target)
  192.  
  193.         if result == "a":
  194.             have_a = True
  195.  
  196.             # Switch rate-up to B
  197.             if target == "a":
  198.                 target = "b"
  199.                 pity_count = 0
  200.  
  201.         elif result == "b":
  202.             have_b = True
  203.  
  204.     return paid_pull_count
  205.  
  206.  
  207. def get_pull_range(pulls, range_size=STEP_SIZE):
  208.     start = ((pulls - 1) // range_size) * range_size + 1
  209.     end = start + range_size - 1
  210.  
  211.     return f"{start}-{end}"
  212.  
  213.  
  214. # ============================================================
  215. # Run simulation
  216. # ============================================================
  217.  
  218.  
  219. def simulate(simulations=SIMULATIONS):
  220.  
  221.     results = {
  222.         "old_rule": Counter(),
  223.         "new_rule": Counter(),
  224.     }
  225.  
  226.     for i in range(simulations):
  227.         if i % 10000 == 0:
  228.             print(i)
  229.  
  230.         pulls = simulate_group_unlimited_old_rule()
  231.         results["old_rule"][get_pull_range(pulls)] += 1
  232.  
  233.         pulls = simulate_group_unlimited_new_rule()
  234.         results["new_rule"][get_pull_range(pulls)] += 1
  235.  
  236.     return results
  237.  
  238.  
  239. # ============================================================
  240. # Write result
  241. # ============================================================
  242.  
  243.  
  244. def write_results(results, filename="gacha_results.txt"):
  245.  
  246.     with open(filename, "w", encoding="utf-8") as file:
  247.         file.write("GACHA SIMULATION RESULTS\n")
  248.         file.write("=" * 80 + "\n\n")
  249.  
  250.         file.write(f"Simulations: {SIMULATIONS:,}\n")
  251.         file.write(f"Rate-up: {RATE_UP * 100:.5f}%\n")
  252.         file.write(f"Off-rate: {RATE_OFF * 100:.5f}%\n\n")
  253.  
  254.         for name, counter in results.items():
  255.             file.write("-" * 80 + "\n")
  256.             file.write(f"{name}\n")
  257.             file.write("-" * 80 + "\n")
  258.  
  259.             total = sum(counter.values())
  260.  
  261.             file.write(f"{'Pull Range':>15} {'Count':>15} {'Probability':>15}\n")
  262.  
  263.             for pull_range, count in sorted(
  264.                 counter.items(),
  265.                 key=lambda x: (
  266.                     float("inf") if x[0] == "FAILED" else int(x[0].split("-")[0])
  267.                 ),
  268.             ):
  269.                 probability = count / total
  270.  
  271.                 file.write(
  272.                     f"{pull_range:>15} {count:>15,} {probability * 100:>14.5f}%\n"
  273.                 )
  274.  
  275.             file.write("\n")
  276.  
  277.  
  278. def plot_cumulative(results, filename="gacha_cumulative.png"):
  279.     plt.figure(figsize=(12, 7))
  280.  
  281.     for name, counter in results.items():
  282.         # Ignore FAILED
  283.         data = {key: value for key, value in counter.items() if key != "FAILED"}
  284.  
  285.         # Sort by pull range
  286.         data = dict(sorted(data.items(), key=lambda x: int(x[0].split("-")[0])))
  287.  
  288.         ranges = list(data.keys())
  289.         counts = list(data.values())
  290.  
  291.         total = sum(counter.values())
  292.  
  293.         # Calculate cumulative probability
  294.         cumulative = []
  295.         current = 0
  296.  
  297.         for count in counts:
  298.             current += count
  299.             cumulative.append(current / total * 100)
  300.  
  301.         # Use the end of each range for X
  302.         x = [int(pull_range.split("-")[1]) for pull_range in ranges]
  303.  
  304.         plt.plot(x, cumulative, label=name.replace("_", " ").title())
  305.  
  306.         for i, (x_value, y_value) in enumerate(zip(x, cumulative)):
  307.             if (i + 1) % GRAPH_LABEL_STEP_SIZE == 0 or (
  308.                 i + 1
  309.             ) in EXTRA_GRAPH_LABEL_STEP:
  310.                 plt.plot(x[i], cumulative[i], marker="o", linestyle="none")
  311.                 plt.annotate(
  312.                     f"{y_value:.2f}%",
  313.                     (x_value, y_value),
  314.                     xytext=(0, 8),
  315.                     textcoords="offset points",
  316.                     ha="center",
  317.                     fontsize=8,
  318.                 )
  319.  
  320.     plt.xlabel("Number of paid pulls")
  321.     plt.ylabel("Cumulative probability (%)")
  322.     plt.title("Cumulative Probability of Getting Both Characters")
  323.  
  324.     plt.grid(axis="both", alpha=0.3)
  325.  
  326.     plt.legend()
  327.  
  328.     plt.tight_layout()
  329.  
  330.     plt.savefig(filename, dpi=150)
  331.  
  332.     plt.close()
  333.  
  334.  
  335. # ============================================================
  336. # Main
  337. # ============================================================
  338.  
  339. if __name__ == "__main__":
  340.     results = simulate()
  341.  
  342.     write_results(results)
  343.  
  344.     plot_cumulative(results)
  345.  
  346.     print("Simulation completed.")
  347.     print("Results written to gacha_results.txt")
  348.  
Advertisement
Add Comment
Please, Sign In to add comment