示例#1
0
        public void Sample(int n, Boolean withReplacement, RandomEngine randomGenerator, Cern.Colt.Buffer.DoubleBuffer buffer)
        {
            if (randomGenerator == null)
            {
                randomGenerator = Cern.Jet.Random.Uniform.MakeDefaultGenerator();
            }
            buffer.Clear();

            if (!withReplacement)
            { // without
                if (n > Size)
                {
                    throw new ArgumentException("n must be less than or equal to Size()");
                }
                Cern.Jet.Random.Sampling.RandomSamplingAssistant sampler = new Cern.Jet.Random.Sampling.RandomSamplingAssistant(n, Size, randomGenerator);
                for (int i = n; --i >= 0;)
                {
                    if (sampler.SampleNextElement())
                    {
                        buffer.Add(this.Elements[i]);
                    }
                }
            }
            else
            { // with
                Cern.Jet.Random.Uniform uniform = new Cern.Jet.Random.Uniform(randomGenerator);
                int s = Size;
                for (int i = n; --i >= 0;)
                {
                    buffer.Add(this.Elements[uniform.NextIntFromTo(0, s - 1)]);
                }
                buffer.Flush();
            }
        }
        /// <summary>
        /// Randomly permutes the part of the receiver between <code>from</code> (inclusive) and <code>to</code> (inclusive)d
        /// <summary>
        /// <param name="from">the index of the first element (inclusive) to be permuted.</param>
        /// <param name="to">the index of the last element (inclusive) to be permuted.</param>
        /// <exception cref="IndexOutOfRangeException">index is out of range (<i>Size&gt;0 && (from&lt;0 || from&gt;to || to&gt;=Size)</i>). </exception>
        public override void ShuffleFromTo(int from, int to)
        {
            // overridden for performance only.
            if (Size == 0)
            {
                return;
            }
            CheckRangeFromTo(from, to, Size);

            var    gen = new Cern.Jet.Random.Uniform(new Cern.Jet.Random.Engine.DRand(new DateTime()));
            double tmpElement;

            double[] theElements = _elements;
            int      random;

            for (int i = from; i < to; i++)
            {
                random = gen.NextIntFromTo(i, to);

                //swap(i, random)
                tmpElement          = theElements[random];
                theElements[random] = theElements[i];
                theElements[i]      = tmpElement;
            }
        }
示例#3
0
        /// <summary>
        /// Randomly permutes the part of the receiver between <code>from</code> (inclusive) and <code>to</code> (inclusive)d
        /// <summary>
        /// <param name="from">the index of the first element (inclusive) to be permuted.</param>
        /// <param name="to">the index of the last element (inclusive) to be permuted.</param>
        /// <exception cref="IndexOutOfRangeException">index is out of range (<i>_size()&gt;0 && (from&lt;0 || from&gt;to || to&gt;=_size())</i>). </exception>
        public override void ShuffleFromTo(int from, int to)
        {
            CheckRangeFromTo(from, to, Size);

            Cern.Jet.Random.Uniform gen = new Cern.Jet.Random.Uniform(Cern.Jet.Random.Uniform.MakeDefaultGenerator());
            for (int i = from; i < to; i++)
            {
                int random = gen.NextIntFromTo(i, to);

                //swap(i, random)
                short tmpElement = GetQuick(random);
                SetQuick(random, GetQuick(i));
                SetQuick(i, tmpElement);
            }
        }
        /// <summary>
        /// Randomly permutes the part of the receiver between <code>from</code> (inclusive) and <code>to</code> (inclusive)d
        /// <summary>
        /// <param name="from">the index of the first element (inclusive) to be permuted.</param>
        /// <param name="to">the index of the last element (inclusive) to be permuted.</param>
        /// <exception cref="IndexOutOfRangeException">index is out of range (<i>_size&gt;0 && (from&lt;0 || from&gt;to || to&gt;=_size)</i>). </exception>
        public override void ShuffleFromTo(int from, int to)
        {
            CheckRangeFromTo(from, to, _size);

            Cern.Jet.Random.Uniform gen = new Cern.Jet.Random.Uniform(new Cern.Jet.Random.Engine.DRand(new DateTime()));
            for (int i = from; i < to; i++)
            {
                int random = gen.NextIntFromTo(i, to);

                //swap(i, random)
                Boolean tmpElement = GetQuick(random);
                SetQuick(random, GetQuick(i));
                SetQuick(i, tmpElement);
            }
        }
示例#5
0
        public static int[] Permutation(long p, int N)
        {
            if (p < 1)
            {
                throw new ArgumentException(Cern.LocalizedResources.Instance().Exception_PermutationsAreEnumerated);
            }
            if (N < 0)
            {
                throw new ArgumentException(Cern.LocalizedResources.Instance().Exception_MustSatisfyNGraterThanOrEqualsToZero);
            }

            int[] permutation = new int[N];

            if (N > 20)
            { // factorial(21) would overflow 64-bit long)
              // Simply make a list (0,1,..N-1) and randomize it, seeded with "p"d
              // Note that this is perhaps not what you want...
                for (int i = N; --i >= 0;)
                {
                    permutation[i] = i;
                }
                var gen = new Cern.Jet.Random.Uniform(new Cern.Jet.Random.Engine.MersenneTwister((int)p));
                for (int i = 0; i < N - 1; i++)
                {
                    int random = gen.NextIntFromTo(i, N - 1);

                    //swap(i, random)
                    int tmp = permutation[random];
                    permutation[random] = permutation[i];
                    permutation[i]      = tmp;
                }

                return(permutation);
            }

            // the normal case - exact enumeration
            if (p > Cern.Jet.Math.Arithmetic.LongFactorial(N))
            {
                throw new ArgumentException(Cern.LocalizedResources.Instance().Exception_NTooLarge);
            }

            var tmp2 = new int[N];

            for (int i = 1; i <= N; i++)
            {
                tmp2[i - 1] = i;
            }

            long io = p - 1;

            for (int M = N - 1; M >= 1; M--)
            {
                long fac = Cern.Jet.Math.Arithmetic.LongFactorial(M);
                int  i   = ((int)(io / fac)) + 1;
                io = io % fac;
                permutation[N - M - 1] = tmp2[i - 1];

                for (int j = i; j <= M; j++)
                {
                    tmp2[j - 1] = tmp2[j];
                }
            }
            if (N > 0)
            {
                permutation[N - 1] = tmp2[0];
            }

            for (int i = N; --i >= 0;)
            {
                permutation[i] = permutation[i] - 1;
            }
            return(permutation);
        }
        public static int[] permutation(long p, int N)
        {
            if (p < 1)
            {
                throw new ArgumentException("Permutations are enumerated 1 .d N!");
            }
            if (N < 0)
            {
                throw new ArgumentException("Must satisfy N >= 0");
            }

            int[] permutation = new int[N];

            if (N > 20)
            { // factorial(21) would overflow 64-bit long)
              // Simply make a list (0,1,..N-1) and randomize it, seeded with "p"d
              // Note that this is perhaps not what you want...
                for (int i = N; --i >= 0;)
                {
                    permutation[i] = i;
                }
                var gen = new Cern.Jet.Random.Uniform(new Cern.Jet.Random.Engine.MersenneTwister((int)p));
                for (int i = 0; i < N - 1; i++)
                {
                    int random = gen.NextIntFromTo(i, N - 1);

                    //swap(i, random)
                    int tmp = permutation[random];
                    permutation[random] = permutation[i];
                    permutation[i]      = tmp;
                }

                return(permutation);
            }

            // the normal case - exact enumeration
            if (p > Cern.Jet.Math.Arithmetic.LongFactorial(N))
            {
                throw new ArgumentException("N too large (a sequence of N elements only has N! permutations).");
            }

            var tmp2 = new int[N];

            for (int i = 1; i <= N; i++)
            {
                tmp2[i - 1] = i;
            }

            long io = p - 1;

            for (int M = N - 1; M >= 1; M--)
            {
                long fac = Cern.Jet.Math.Arithmetic.LongFactorial(M);
                int  i   = ((int)(io / fac)) + 1;
                io = io % fac;
                permutation[N - M - 1] = tmp2[i - 1];

                for (int j = i; j <= M; j++)
                {
                    tmp2[j - 1] = tmp2[j];
                }
            }
            if (N > 0)
            {
                permutation[N - 1] = tmp2[0];
            }

            for (int i = N; --i >= 0;)
            {
                permutation[i] = permutation[i] - 1;
            }
            return(permutation);
        }