20 lines
1.9 KiB
Plaintext
20 lines
1.9 KiB
Plaintext
###############################################
|
|
Prompt to Kiro AI (Default):
|
|
|
|
im getting a list index out of range error very rarely, right as the fishing timer reaches zero and it begins the catching process. any idea what could be causing that? dont fix, explain.
|
|
|
|
Response:
|
|
|
|
It's almost certainly the pop() loop in pickRandomCreature.
|
|
|
|
The loop pops until roll drops to 0 or below. The guard roll = random.random() * (total - 0.0001) is meant to guarantee the roll is always strictly less than total, so the loop empties enough weight before running out of items. But floating-point rounding breaks that guarantee in rare cases.
|
|
|
|
Here's the problem: total is the sum of all the individual weights. Due to float accumulation error, sum(weights) is not exactly equal to subtracting those same weights one by one. In the unlucky case where roll lands extremely close to total, you can pop every item in possibilities and roll is still above 0 by a tiny epsilon — then the next .pop() hits an empty list → index out of range.
|
|
|
|
The - 0.0001 fudge shrinks the odds but doesn't eliminate them: if the summed total is itself slightly larger than the re-subtracted total, that 0.0001 margin can be smaller than the accumulated error, especially with your sqrt'd weights which are non-round floats.
|
|
|
|
Why it's rare and tied to the timer hitting zero: pickRandomCreature only runs at that moment (bite triggers the pick), and it only fails when the random roll lands in that thin top sliver near total. Most rolls land comfortably in the middle, so you almost never see it.
|
|
|
|
Side note that makes it worse: you pop() from the end of the list every time, which is fine mechanically, but it means the walk order and the weight order are the same list being mutated — so once it's empty there's no fallback. A for-loop over a fixed list with a final default pick would avoid the empty-list crash entirely.
|
|
|
|
############################################### |