Java Random extractRandomVector(Collection c, int newSize)

Here you can find the source of extractRandomVector(Collection c, int newSize)

Description

extract Random Vector

License

Open Source License

Declaration

public static <K> Vector<K> extractRandomVector(Collection<K> c, int newSize) 

Method Source Code


//package com.java2s;
/*/*from  w w  w .j  ava  2 s  . c om*/
 * WANDORA
 * Knowledge Extraction, Management, and Publishing Application
 * http://wandora.org
 * 
 * Copyright (C) 2004-2016 Wandora Team
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * 
 *
 * GenericVelocityHelper.java
 *
 * Created on 17. joulukuuta 2004, 10:54
 */

import java.util.*;

public class Main {
    public static <K> Vector<K> extractRandomVector(Collection<K> c, int newSize) {
        Vector cropped = new Vector();
        if (c == null || c.size() < 1 || newSize < 1)
            return cropped;
        int s = c.size();
        int randomStep = 0;
        Iterator<K> iter = c.iterator();
        for (int i = 0; i < newSize; i++) {
            randomStep = (int) Math.floor(Math.random() * (s / newSize)) - 1;
            while (iter.hasNext() && randomStep-- > 0)
                iter.next();
            if (iter.hasNext())
                cropped.add(iter.next());
        }
        return cropped;
    }
}

Related

  1. createRandomStr(int digit)
  2. decimal_random()
  3. equalsRandomIterable(List a, List bl)
  4. extractRandomObject(Collection c)
  5. gaussianInt(Random random, int min, int max)
  6. generateRandom(byte[] byteArray)
  7. generateRandom(int length, int type)
  8. generateRandom(Integer bitNum)