OpenSim.Region.ScriptEngine.Shared.Api.LSL_Api.llListRandomize C# (CSharp) Method

llListRandomize() public method

Randomizes the list, be arbitrarily reordering sublists of stride elements. As the stride approaches the size of the list, the options become very limited.
This could take a while for very large list sizes.
public llListRandomize ( OpenSim.Region.ScriptEngine.Shared.LSL_Types.list src, int stride ) : OpenSim.Region.ScriptEngine.Shared.LSL_Types.list
src OpenSim.Region.ScriptEngine.Shared.LSL_Types.list
stride int
return OpenSim.Region.ScriptEngine.Shared.LSL_Types.list
        public LSL_List llListRandomize(LSL_List src, int stride)
        {
            LSL_List result;
            BetterRandom rand = new BetterRandom();

            int   chunkk;
            int[] chunks;

            m_host.AddScriptLPS(1);

            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)
            {
                chunkk = src.Length/stride;

                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 > 0; 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;
        }
LSL_Api