解决方案 »

  1.   

    把这100个集合对象装到一个集合里!循环它,用random拿到随机数做为随机角标!取值!剩下一个数你自己想吧!
      

  2.   

    不知道,理解对不对
    import java.security.SecureRandom;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Random;public class RandomText {  public static void main(String[] args) {
        String[] dictionaries = new String[] {
            "abcdefghijklmnopqrstuvwxyz", // 小写
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ", // 大写
            "0123456789", // 数字
            "!@#$%^" // 符号
        };
        System.out.println(randomText(dictionaries, 5));
      }  /**
       * 返回随机字符串。
       * 
       * @param dictionaries
       *          不同的字典组合,每种都要至少出现一次
       * @param length
       *          最后随机字符串长度
       * @return 随机字符串
       * @throws IllegalArgumentException
       *           如果字典数量或者长度限制不足以满足题意的话
       */
      public static String randomText(String[] dictionaries, int length) {
        if (length < dictionaries.length) {
          throw new IllegalArgumentException("length < dictionaries.length");
        }    // 被选中的字符列表,事后还要打乱顺序
        List<Character> selected = new ArrayList<Character>(length);
        // 所有种类组合在一起的字典列表(扣除一开始已经被选中的,见下面代码解释)
        List<Character> rest = new LinkedList<Character>();    Random random = new SecureRandom();
        for (String dictionary : dictionaries) {
          List<Character> list = new LinkedList<Character>();
          for (char c : dictionary.toCharArray()) {
            list.add(c);
          }
          // 由于每种必然出现一次,先抽取一下
          selected.add(list.remove(random.nextInt(list.size())));
          // 剩余的这次未被选中的,组合在一起
          rest.addAll(list);
        }    // 再给未被选中的一些机会
        while (selected.size() < length) {
          selected.add(rest.remove(random.nextInt(rest.size())));
        }    // 至此,所有被选中的,都已在selected里面,剩下只要打乱顺序即可
        Collections.shuffle(selected, random);
        
        StringBuilder buff = new StringBuilder(length);
        for (char c : selected) {
          buff.append(c);
        }
        return buff.toString();
      }
      

  3.   

    这位大哥一点了还不休息,注意身体呀。
    你这个代码和我一开始想的一样,我其实想问的就是打乱的那句Collections.shuffle如何在前面取这5个字符的时候一起实现,而不是在取完之后再实现打乱。
      

  4.   

    遍历元素最少的集合,每个集合先随机取一个位置的字符,再随机取一个集合的随机位置的一个字符,当字符数=101的时候就return
      

  5.   

      /**
       * 返回随机字符串。
       * 
       * @param dictionaries
       *          不同的字典组合,每种都要至少出现一次
       * @param length
       *          最后随机字符串长度
       * @return 随机字符串
       * @throws IllegalArgumentException
       *           如果字典数量或者长度限制不足以满足题意的话
       */
      public static String randomText2(String[] dictionaries, int length) {
        if (length < dictionaries.length) {
          throw new IllegalArgumentException("length < dictionaries.length");
        }    Random random = new SecureRandom();    // 每种类型出现的次数
        int[] count = new int[dictionaries.length];
        // 所有字典
        List<List<Character>> allDictionaries = new ArrayList<List<Character>>(
            dictionaries.length);
        for (int i = 0; i < dictionaries.length; i++) {
          // 每种至少出现一次
          count[i] = 1;
          List<Character> list = new LinkedList<Character>();
          for (char c : dictionaries[i].toCharArray()) {
            list.add(c);
          }
          allDictionaries.add(list);
        }    // 剩余次数随机分配
        for (int i = length - dictionaries.length; i-- > 0;) {
          count[random.nextInt(dictionaries.length)]++;
        }    char[] chars = new char[length];    // 每一位的字符分配
        for (int i = 0; i < length; i++) {
          // 种类
          int type = random.nextInt(dictionaries.length);
          // 一直找直到找到有还没剩余次数的为止
          while (count[type] == 0) {
            type++;
            type %= dictionaries.length;
          }
          count[type]--;
          List<Character> dictionary = allDictionaries.get(type);
          chars[i] = dictionary.remove(random.nextInt(dictionary.size()));
        }    return new String(chars);
      }
      

  6.   

    BUG修正
      /**
       * 返回随机字符串。
       * 
       * @param dictionaries
       *          不同的字典组合,每种都要至少出现一次
       * @param length
       *          最后随机字符串长度
       * @return 随机字符串
       * @throws IllegalArgumentException
       *           如果字典数量或者长度限制不足以满足题意的话
       */
      public static String randomText2(String[] dictionaries, int length) {
        if (length < dictionaries.length) {
          throw new IllegalArgumentException("length < dictionaries.length");
        }    Random random = new SecureRandom();    // 每种类型出现的次数
        int[] count = new int[dictionaries.length];
        // 所有字典
        List<List<Character>> allDictionaries = new ArrayList<List<Character>>(
            dictionaries.length);
        // 所有字符总数
        int totalCount = 0;
        for (int i = 0; i < dictionaries.length; i++) {
          // 每种至少出现一次
          count[i] = 1;
          List<Character> list = new LinkedList<Character>();
          for (char c : dictionaries[i].toCharArray()) {
            list.add(c);
          }
          allDictionaries.add(list);
          totalCount += list.size();
        }
        
        if (totalCount < length) {
          throw new IllegalArgumentException("totalCount < length");
        }    // 剩余次数随机分配
        for (int i = length - dictionaries.length; i-- > 0;) {
          count[random.nextInt(dictionaries.length)]++;
        }    char[] chars = new char[length];    // 每一位的字符分配
        for (int i = 0; i < length; i++) {
          // 种类
          int type = random.nextInt(dictionaries.length);
          // 一直找直到找到空位为止
          while (count[type] == 0 || count[type] == dictionaries[i].length()) {
            type++;
            type %= dictionaries.length;
          }
          count[type]--;
          List<Character> dictionary = allDictionaries.get(type);
          chars[i] = dictionary.remove(random.nextInt(dictionary.size()));
        }    return new String(chars);
      }