reginafeles's picture
Upload 40 files
d2213a5 verified
"""Генератор задач: бросание игральных костей"""
from core.base_generator import BaseTaskGenerator
from models.task import Task
from fractions import Fraction
from config import COMPLEXITY_CONFIG
from utils.dice_utils import DiceCombinations
import random
class DiceGenerator(BaseTaskGenerator):
"""Генератор задач про бросание костей"""
def generate(self) -> Task:
"""Генерация задачи"""
config = COMPLEXITY_CONFIG[self.complexity]
num_dice = random.choice([2, 3])
if num_dice == 2:
combinations = DiceCombinations.get_two_dice_combinations()
total_outcomes = 36
target_sum = random.randint(2, 12)
else:
combinations = DiceCombinations.get_three_dice_combinations()
total_outcomes = 216
target_sum = random.randint(3, 18)
favorable = combinations.get(target_sum, 0)
question = f"В случайном эксперименте бросают {num_dice} игральные кости. Найдите вероятность того, что в сумме выпадет {target_sum} очков."
answer_fraction = Fraction(favorable, total_outcomes)
answer = round(float(answer_fraction), 2)
steps = [
f"1. Общее число исходов: 6^{num_dice} = {total_outcomes}",
f"2. Благоприятные исходы (сумма {target_sum}): {favorable}",
f"3. Вероятность = {favorable}/{total_outcomes} = {answer_fraction}"
]
return Task(
type='dice',
question=question,
answer=answer,
answer_fraction=str(answer_fraction),
solution=f"Вероятность = {favorable}/{total_outcomes} = {answer_fraction}",
steps=steps,
complexity=self.complexity
)
def get_type(self) -> str:
return 'dice'