From fc8a9e362c4460f9178aabd45495cd3d8ddb8419 Mon Sep 17 00:00:00 2001 From: SkyratBot <59378654+SkyratBot@users.noreply.github.com> Date: Wed, 16 Nov 2022 11:15:59 +0100 Subject: [PATCH] [MIRROR] Fixes pick_weight() [MDB IGNORE] (#17501) * Fixes pick_weight() (#71273) ## About The Pull Request Good lord, how many years was this proc mathematically broken in such a basic way? Fixes #71271 The issue was that whoever wrote pick_weight either didn't know how rand() worked, how byond list indexing worked, or was high. ### Results from re-running the test procs given in the linked issue Choosing from `list("a"=1, "b"=1)` (should be 50/50) - A: 985 (49%) - B: 1016 (51%) Choosing from `list("a"=1, "b"=1, "c"=1, "d"=1, "e"=1, "f"=1, "g"=1, "h"=1)` (each letter should have 12.5%) - A: 268 (13%) - B: 256 (13%) - C: 249 (12%) - D: 272 (14%) - E: 242 (12%) - F: 240 (12%) - G: 237 (12%) - H: 237 (12%) Choosing from `list("c"=1, "b"=1, "a"=1)` (should be 33/33/33) - C: 658 (33%) - B: 674 (34%) - A: 669 (33%) Choosing from `list("a"=3, "b"=2, "c"=5`) (should be 30/20/50 -- note the bias towards "a" despite the large number of samples) - A: 603 (30%) - B: 400 (20%) - C: 998 (50%) Choosing from `list("a"=0, "b"=1, "c"=1)` (should be 0/50/50 -- note that the bias towards "a" is instead shifted towards "b". The zero weight takes priority, apparently) - A: 0 (0%) - B: 993 (50%) - C: 1008 (50%) ## Why It's Good For The Game Basic core procs working as expected ## Changelog Not client facing unless people are crunching some meta really hard. * Fixes pick_weight() Co-authored-by: Tastyfish --- code/__HELPERS/_lists.dm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/__HELPERS/_lists.dm b/code/__HELPERS/_lists.dm index 2abc292228a..3cb39a1a5ac 100644 --- a/code/__HELPERS/_lists.dm +++ b/code/__HELPERS/_lists.dm @@ -419,7 +419,7 @@ list_to_pick[item] = 0 total += list_to_pick[item] - total = rand(0, total) + total = rand(1, total) for(item in list_to_pick) total -= list_to_pick[item] if(total <= 0 && list_to_pick[item])