Mantis#1831.Thank you kindly, Francis for a patch that addresses:

Not all combinations of list order equally likely with llListRandomize()
0.6.0-stable
Charles Krinke 2008-07-27 16:21:51 +00:00
parent f417a03414
commit e4ef774875
2 changed files with 18 additions and 28 deletions

View File

@ -3803,9 +3803,6 @@ namespace OpenSim.Region.ScriptEngine.Common
int chunkk; int chunkk;
int[] chunks; int[] chunks;
int index1;
int index2;
int tmp;
m_host.AddScriptLPS(1); m_host.AddScriptLPS(1);
@ -3825,18 +3822,17 @@ namespace OpenSim.Region.ScriptEngine.Common
for (int i = 0; i < chunkk; i++) for (int i = 0; i < chunkk; i++)
chunks[i] = 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 // Elect an unrandomized chunk to swap
index1 = rand.Next(rand.Next(65536)); int index = rand.Next(i + 1);
index1 = index1%chunkk; int tmp;
index2 = rand.Next(rand.Next(65536));
index2 = index2%chunkk;
// and swap their relative positions // and swap position with first unrandomized chunk
tmp = chunks[index1]; tmp = chunks[i];
chunks[index1] = chunks[index2]; chunks[i] = chunks[index];
chunks[index2] = tmp; chunks[index] = tmp;
} }
// Construct the randomized list // Construct the randomized list

View File

@ -3736,15 +3736,11 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
public LSL_Types.list llListRandomize(LSL_Types.list src, int stride) public LSL_Types.list llListRandomize(LSL_Types.list src, int stride)
{ {
LSL_Types.list result; LSL_Types.list result;
Random rand = new Random(); Random rand = new Random();
int chunkk; int chunkk;
int[] chunks; int[] chunks;
int index1;
int index2;
int tmp;
m_host.AddScriptLPS(1); m_host.AddScriptLPS(1);
@ -3764,18 +3760,17 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
for (int i = 0; i < chunkk; i++) for (int i = 0; i < chunkk; i++)
chunks[i] = 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 // Elect an unrandomized chunk to swap
index1 = rand.Next(rand.Next(65536)); int index = rand.Next(i+1);
index1 = index1%chunkk; int tmp;
index2 = rand.Next(rand.Next(65536));
index2 = index2%chunkk;
// and swap their relative positions // and swap position with first unrandomized chunk
tmp = chunks[index1]; tmp = chunks[i];
chunks[index1] = chunks[index2]; chunks[i] = chunks[index];
chunks[index2] = tmp; chunks[index] = tmp;
} }
// Construct the randomized list // Construct the randomized list
@ -3797,7 +3792,6 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
} }
return result; return result;
} }
/// <summary> /// <summary>