feat: génération pondérée des questions (les grands facteurs sortent plus souvent)

This commit is contained in:
herel committed 2026-08-31 09:35:11 +02:00
1 parent b454fbd0d7
commit 691c98f862
3 files changed
+52 -3

No files matched your search

@@ -9,11 +9,21 @@ fun interface QuestionSource {
class QuestionGenerator(private val random: Random = Random.Default) : QuestionSource {
override fun next(previous: Question?, maxTable: Int): Question {
while (true) {
val a = random.nextInt(1, maxTable + 1)
val b = random.nextInt(1, maxTable + 1)
val a = nextFactor(maxTable)
val b = nextFactor(maxTable)
val question =
if (random.nextBoolean()) Question.Direct(a, b) else Question.MissingFactor(a, b)
if (question != previous) return question
}
}
private fun nextFactor(maxTable: Int): Int {
val totalWeight = maxTable * (maxTable + 1) / 2
var roll = random.nextDouble() * totalWeight
for (k in 1..maxTable) {
roll -= k
if (roll < 0) return k
}
return maxTable
}
}
@@ -58,4 +58,43 @@ class QuestionGeneratorTest {
assertEquals("7 × 8 = ?", Question.Direct(7, 8).toString())
assertEquals("7 × ? = 56", Question.MissingFactor(7, 8).toString())
}
private fun factors(q: Question): Pair<Int, Int> = when (q) {
is Question.Direct -> q.a to q.b
is Question.MissingFactor -> q.a to q.b
}
@Test
fun `biais progressif - les facteurs eleves reviennent plus souvent`() {
val generator = QuestionGenerator(Random(42))
var with1 = 0
var with10 = 0
repeat(5000) {
val (a, b) = factors(generator.next(previous = null, maxTable = 10))
if (a == 1 || b == 1) with1++
if (a == 10 || b == 10) with10++
}
assertTrue(with1 < with10 / 5, "with1=$with1, with10=$with10")
}
@Test
fun `les petits facteurs restent possibles`() {
val generator = QuestionGenerator(Random(42))
var with1 = 0
repeat(5000) {
val (a, b) = factors(generator.next(previous = null, maxTable = 10))
if (a == 1 || b == 1) with1++
}
assertTrue(with1 > 0, "with1=$with1")
}
@Test
fun `maxTable egale a 1 - seule question possible sans boucle infinie`() {
val generator = QuestionGenerator(Random(42))
repeat(200) {
val (a, b) = factors(generator.next(previous = null, maxTable = 1))
assertEquals(1, a)
assertEquals(1, b)
}
}
}