public LSL_List llListRandomize(LSL_List src, int stride)
{
LSL_List result;
Random rand = new Random();
if (!ScriptProtection.CheckThreatLevel(ThreatLevel.None, "LSL", m_host, "LSL", m_itemID)) return new LSL_List();
if (stride <= 0)
{
stride = 1;
}
// Stride MUST be a factor of the list length
// If not, then return the src list. This also
// traps those cases where stride > length.
if (src.Length != stride && src.Length % stride == 0)
{
int chunkk = src.Length / stride;
int[] chunks = new int[chunkk];
for (int i = 0; i < chunkk; i++)
chunks[i] = i;
// Knuth shuffle the chunkk index
for (int i = chunkk - 1; i >= 1; i--)
{
// Elect an unrandomized chunk to swap
int index = rand.Next(i + 1);
// and swap position with first unrandomized chunk
int tmp = chunks[i];
chunks[i] = chunks[index];
chunks[index] = tmp;
}
// Construct the randomized list
result = new LSL_List();
for (int i = 0; i < chunkk; i++)
{
for (int j = 0; j < stride; j++)
{
result.Add(src.Data[chunks[i] * stride + j]);
}
}
}
else
{
object[] array = new object[src.Length];
Array.Copy(src.Data, 0, array, 0, src.Length);
result = new LSL_List(array);
}
return result;
}