Skip to contentSkip to conversation

Improve shuffling algorithm of connection list

PATCH New Hurukawa2121 1 participant 1 message Nov 15, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Improve shuffling algorithm of connection list

From: Hurukawa2121 <[email protected]...>

---
Improve shuffling algorithm of connection list

This patch implements the Fisher-Yates shuffle algorithm to ensure that all permutations of the connection target list are generated with equal probability, eliminating biases present in the previous shuffling method.
In the Fisher-Yates algorithm, there's only one way to obtain each permutation through these swaps, so all permutations occur with equal probability in theory.

 src/openvpn/init.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/openvpn/init.c +2 -2
index 9371024e..3b00b49d 100644
@@ -478,9 +478,9 @@ init_connection_list(struct context *c)
     if (c->options.remote_random)
     {
         int i;
-        for (i = 0; i < l->len; ++i)
+        for (i = l->len - 1; i > 0; --i)
         {
-            const int j = get_random() % l->len;
+            const int j = get_random() % (i + 1);
             if (i != j)
             {
                 struct connection_entry *tmp;
Signature · 1 line
2.39.3 (Apple Git-146)