|
1 | 1 | import asyncio |
2 | | -import random |
| 2 | +import secrets |
3 | 3 | from collections import defaultdict |
4 | 4 | from functools import partial |
5 | 5 | from typing import Any, Callable, Awaitable |
|
12 | 12 |
|
13 | 13 | POT_ENTRY_COST = 5 |
14 | 14 | DAILY_DRAW_CHANCE = 0.95 |
15 | | -RANDOM_WIN_CHANCE = 0.025 |
| 15 | +RANDOM_WIN_CHANCE = 0.01 |
16 | 16 | AUTO_ENTER_DELAY_SECONDS = 30 |
17 | 17 |
|
18 | 18 | # Per-guild lock to serialize pot mutations (entries, instant wins, draws). |
@@ -80,7 +80,7 @@ async def enter_pot( |
80 | 80 | # Roll for instant win BEFORE creating a payment request. |
81 | 81 | # An instant win skips payment entirely — the user wins the |
82 | 82 | # current pot for free and the pot ends immediately. |
83 | | - if random.random() < RANDOM_WIN_CHANCE: |
| 83 | + if secrets.randbelow(10000) < RANDOM_WIN_CHANCE * 10000: |
84 | 84 | participants = db.get_pot_participants(conn, pot_id) |
85 | 85 | total_pot = sum(p["amount"] for p in participants) |
86 | 86 | logger.info( |
@@ -213,7 +213,7 @@ def select_random_winner(participants: list[dict]) -> dict | None: |
213 | 213 | return None |
214 | 214 |
|
215 | 215 | total_weight = sum(max(p["amount"], POT_ENTRY_COST) for p in participants) |
216 | | - roll = random.uniform(0, total_weight) |
| 216 | + roll = secrets.randbelow(total_weight + 1) |
217 | 217 | cumulative = 0 |
218 | 218 | for p in participants: |
219 | 219 | cumulative += max(p["amount"], POT_ENTRY_COST) |
@@ -436,10 +436,10 @@ async def daily_pot_draw( |
436 | 436 | async with _guild_locks[guild_id]: |
437 | 437 | conn = db.get_connection() |
438 | 438 | try: |
439 | | - roll = random.random() |
440 | | - if roll < DAILY_DRAW_CHANCE: |
| 439 | + roll = secrets.randbelow(10000) |
| 440 | + if roll < DAILY_DRAW_CHANCE * 10000: |
441 | 441 | logger.info( |
442 | | - f"Daily draw triggered for guild {guild_id} (roll={roll:.3f})" |
| 442 | + f"Daily draw triggered for guild {guild_id} (roll={roll}/10000)" |
443 | 443 | ) |
444 | 444 | guild_announce = partial(announce, guild_id) if announce else None |
445 | 445 | guild_edit = ( |
|
0 commit comments