Ejemplo n.º 1
0
        /// <summary>
        ///   Returns a random sample of size k from a population of size n.
        /// </summary>
        public static int[] Random(int n, int k)
        {
            var r = new Random();

            // If the sample is a sizeable fraction of the population, just
            // randomize the whole population (which involves a full sort
            // of n random values), and take the first k.
            if (4 * k > n)
            {
                int[] idx = Tools.Random(n);
                return(idx.Submatrix(k));
            }
            else
            {
                int[] x = new int[n];

                int sumx = 0;
                while (sumx < k)
                {
                    x[r.Next(0, n)] = 1;
                    sumx            = Accord.Math.Matrix.Sum(x);
                }

                int[] y = x.Find(z => z > 0);
                return(y.Submatrix(Random(k)));
            }
        }