From e4ef7748753489792e86f14fa7d8bace37ef8590 Mon Sep 17 00:00:00 2001
From: Charles Krinke
Date: Sun, 27 Jul 2008 16:21:51 +0000
Subject: Mantis#1831.Thank you kindly, Francis for a patch that addresses: Not
 all combinations of list order equally likely with llListRandomize()

---
 .../ScriptEngine/Common/LSL_BuiltIn_Commands.cs    | 24 +++++++++-----------
 .../Shared/Api/Implementation/LSL_Api.cs           | 26 +++++++++-------------
 2 files changed, 20 insertions(+), 30 deletions(-)

(limited to 'OpenSim')

diff --git a/OpenSim/Region/ScriptEngine/Common/LSL_BuiltIn_Commands.cs b/OpenSim/Region/ScriptEngine/Common/LSL_BuiltIn_Commands.cs
index a67e7ab..684c0cb 100644
--- a/OpenSim/Region/ScriptEngine/Common/LSL_BuiltIn_Commands.cs
+++ b/OpenSim/Region/ScriptEngine/Common/LSL_BuiltIn_Commands.cs
@@ -3803,9 +3803,6 @@ namespace OpenSim.Region.ScriptEngine.Common
 
             int   chunkk;
             int[] chunks;
-            int   index1;
-            int   index2;
-            int   tmp;
 
             m_host.AddScriptLPS(1);
 
@@ -3825,18 +3822,17 @@ namespace OpenSim.Region.ScriptEngine.Common
                 for (int i = 0; i < chunkk; i++)
                     chunks[i] = i;
 
-                for (int i = 0; i < chunkk - 1; i++)
+                // Knuth shuffle the chunkk index
+                for (int i = chunkk - 1; i >= 1; i--)
                 {
-                    //  randomly select 2 chunks
-                    index1 = rand.Next(rand.Next(65536));
-                    index1 = index1%chunkk;
-                    index2 = rand.Next(rand.Next(65536));
-                    index2 = index2%chunkk;
-
-                    //  and swap their relative positions
-                    tmp = chunks[index1];
-                    chunks[index1] = chunks[index2];
-                    chunks[index2] = tmp;
+                    // Elect an unrandomized chunk to swap
+                    int index = rand.Next(i + 1);
+                    int tmp;
+
+                    // and swap position with first unrandomized chunk
+                    tmp = chunks[i];
+                    chunks[i] = chunks[index];
+                    chunks[index] = tmp;
                 }
 
                 // Construct the randomized list
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs
index ed9bebf..4438395 100644
--- a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs
+++ b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs
@@ -3736,15 +3736,11 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
 
         public LSL_Types.list llListRandomize(LSL_Types.list src, int stride)
         {
-
             LSL_Types.list result;
             Random rand           = new Random();
 
             int   chunkk;
             int[] chunks;
-            int   index1;
-            int   index2;
-            int   tmp;
 
             m_host.AddScriptLPS(1);
 
@@ -3764,18 +3760,17 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
                 for (int i = 0; i < chunkk; i++)
                     chunks[i] = i;
 
-                for (int i = 0; i < chunkk - 1; i++)
+                // Knuth shuffle the chunkk index
+                for (int i = chunkk-1; i >= 1; i--)
                 {
-                    //  randomly select 2 chunks
-                    index1 = rand.Next(rand.Next(65536));
-                    index1 = index1%chunkk;
-                    index2 = rand.Next(rand.Next(65536));
-                    index2 = index2%chunkk;
-
-                    //  and swap their relative positions
-                    tmp = chunks[index1];
-                    chunks[index1] = chunks[index2];
-                    chunks[index2] = tmp;
+                    // Elect an unrandomized chunk to swap
+                    int index = rand.Next(i+1);
+                    int tmp;
+
+                    // and swap position with first unrandomized chunk
+                    tmp = chunks[i];
+                    chunks[i] = chunks[index];
+                    chunks[index] = tmp;
                 }
 
                 // Construct the randomized list
@@ -3797,7 +3792,6 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
             }
 
             return result;
-
         }
 
         /// <summary>
-- 
cgit v1.1