Java Random String randomSubset(String[] list, int count)

Here you can find the source of randomSubset(String[] list, int count)

Description

Returns a subset of the list supplied.

License

Open Source License

Parameter

Parameter Description
count the number of Strings you want returned in the subset

Declaration


public static String[] randomSubset(String[] list, int count) 

Method Source Code

//package com.java2s;
/*//www  .  ja v  a2 s . c o m
 * Copyright: (c) 2002-2006 Mayo Foundation for Medical Education and
 * Research (MFMER).  All rights reserved.  MAYO, MAYO CLINIC, and the
 * triple-shield Mayo logo are trademarks and service marks of MFMER.
 *
 * Except as contained in the copyright notice above, the trade names, 
 * trademarks, service marks, or product names of the copyright holder shall
 * not be used in advertising, promotion or otherwise in connection with
 * this Software without prior written authorization of the copyright holder.
 * 
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 *       http://www.eclipse.org/legal/epl-v10.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.*;

public class Main {
    /**Returns a subset of the list supplied.  The items in the
    returned are non-repeated.
    @param count the number of Strings you want returned in the subset
    */

    public static String[] randomSubset(String[] list, int count) {
        if (list.length < count)
            return null;

        Vector vList = new Vector();
        for (int i = 0; i < list.length; i++) {
            vList.addElement(list[i]);
        }

        Random random = new Random(System.currentTimeMillis());

        String RandomItems[] = new String[count];
        int index;
        for (int i = 0; i < count; i++) {
            index = random.nextInt(vList.size());
            RandomItems[i] = (String) (vList.elementAt(index));
            vList.removeElementAt(index);
        }

        return RandomItems;

    }
}

Related

  1. randomString(int length)
  2. randomstring(int lo, int hi)
  3. randomString(int stringLength)
  4. randomString(Integer len)
  5. randomString(Random r, int minCount, int maxCount)
  6. randomSuffix(final String name)
  7. randomUnicodeString(Random r)
  8. Shuffle(ArrayList list, Random randomNumberGenerator)