From: Hurukawa2121 <shujifurukawa1...@gamil.com>

---
 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.

 Signed-off-by: Hurukawa2121 <shujifurukawa1...@gmail.com>

 src/openvpn/init.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/openvpn/init.c b/src/openvpn/init.c
index 9371024e..3b00b49d 100644
--- a/src/openvpn/init.c
+++ b/src/openvpn/init.c
@@ -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;
-- 
2.39.3 (Apple Git-146)



_______________________________________________
Openvpn-devel mailing list
Openvpn-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to