java-输出不同的随机字符串

时间:2014-06-13 12:57:10

标签: java string random

我是java新手,我很震惊。

我有一个方法可以生成1-13到' cdsh'之间的随机字符串。将这两个组合在一起,它将确定我将在程序中收到的卡片类型。

我会随机抽出四次,如果有任何结果相同, 该程序将再次随机。

 e.g random output 
    s1(spades of 1)
    h3(hearts of 3)
    s1(spades of 1) <-- duplicated! it will random again and produce and different string.
    d1(diamond of 1)  

public static String randomizedCard() {
    int randomInt;
    String rank = null;
    String suits = null;
    String cardType; 
    Random randomGenerator = new Random();  
    randomInt = randomGenerator.nextInt(13);

    if(randomInt == 0)
    {
    randomInt = randomGenerator.nextInt(13);
    }

    rank = Integer.toString(randomInt);

    char[] chars = "cdhs".toCharArray();
    StringBuilder sb = new StringBuilder();
    Random randomChar = new Random();

    char c = chars[randomChar.nextInt(chars.length)];
    sb.append(c);
    suits = sb.toString();

    cardType = suits + rank;
    System.out.println(cardType);
    return cardType;
}


public static void main(String[] args) {    

        String[] ic = new String[4];
        for(int i = 0; i < 4; i++) {

            /*from here onwards I get confused on how should I write the code such that
              if it randoms the same string, it will the random method (randomizedCard)
              till it produce all four different random numbers without duplicate and store
              it into the array.*/                  

            //store the random string to an array
            ic[i] = randomizedCard();
            // if it's the same random again 
            if(ic[i] == randomsizedCard()) {
                randomsizedCard();
                ic[i] = randomsizedCard();
            }

        }          
    }

我应该如何编写代码,使得如果randoms相同的字符串,它将是随机方法(randomizedCard),直到它产生所有四个不重复的随机数并将其存储到数组中?

5 个答案:

答案 0 :(得分:4)

对于这种事情,有一种更简单的方法:

创建List<Card> cards;

用每张可能的卡填写清单:

for (String suit: suites) {
    for (int i=1;i<=13;i++) {
        cards.add(new Card(suit, i));
    }
}

然后洗牌:

Collections.shuffle(cards);

现在只需从列表中取出前X个项目,即可保证每个项目都是随机且唯一的。

答案 1 :(得分:1)

将随机生成的卡片放入ArrayList中,然后检查每张新卡片与其他卡片的对比情况。

ArrayList<String> cards = new ArrayList();
while (cards.size() < 4) {
    String card = randomizedCard();
    if (!cards.contains(card)) {
        cards.add(card);
    }
}

答案 2 :(得分:0)

检查卡是否已经创建,如果是,请创建其他人,直到您有4。

List<String> cards = new ArrayList<>();
while ( cards.size() < 4 )
{
     String newCard = randomizedCard();
     if ( !cards.contains( newCard ) )
     {
           cards.add( newCard );
     }
}

答案 3 :(得分:0)

好随机是随机的。

如果你想随意而不重复,就这样做。

创建卡片对象

public class Card {
    int seed;//0-3, the card seed 
    int number;//1-10 the card number
    public Card(seed, number) {
        this.seed =seed;
        this.number = number;
    }
    public String toString() {
       return "card " + number + " of " + seed; //here you convert from int to beautiful seed name
    }
}

将牌组中的所有牌创建为集合

LinkedList deck = new LinkedList();
for (int seed = 0; seed<4; seed++)     for (int number = 1; number<11; number++) { 
  deck.add(new Card(seed,number));
}

shuffle the cards

Collections.shuffle(deck);

使用pop mechanism (return and remove one)选择前四个:

System.out.println(deck.pop());
System.out.println(deck.pop());
System.out.println(deck.pop());
System.out.println(deck.pop());

答案 4 :(得分:0)

public String getString(int x){
    List<String> list = new ArrayList<String>();

    list.add("*******Add your string here****** ");
    list.add("*******Add your string here****** ");

    HashMap<Integer, String> hashMap = new HashMap<>();

    Collections.shuffle(list);
    for (int i = 0; i<5; i++){
        hashMap.put( i, list.get(i));
    }

    return hashMap.get(x);
 }

在主函数中调用带有参数0,1,2 ...的getString(),它将随机返回字符串,而无需重复。