简体   繁体   English

随机选择一组双打

[英]Randomly select a group of doubles

I have a list of doubles, for the purpose of this lets say I have doubles named one, two, three, four, five, six, seven, etc...through 100. Each one is set to a value. 我有一个双打列表,为了这个目的,我可以说我有一个,两个,三个,四个,五个,六个,七个等等的双打......到100.每个都设置为一个值。 (the values don't correspond with the names). (这些值与名称不对应)。

How can I randomly select 10 of these, and set each of the selected ones to be another double as well? 如何随机选择其中的10个,并将每个选定的设置为另一个?

EG Say double "one" is selected, along with 9 others. EG说选择双“一”,以及另外9个。

I want to also set the double "blah" to the value of the selected double. 我还想将双“blah”设置为所选双精度值。 I then want to set the double "blahtwo" to the value of the next selected double, etc... 然后我想将双“blahtwo”设置为下一个选定的double的值,等等...

I do not want to randomly select the same double twice. 我不想随机选择两次相同的双倍。

Example code: 示例代码:

  double cardone, cardtwo, cardthree, cardfour, cardfive, cardsix, cardseven, cardeight, cardnine, cardten, cardeleven, cardtwelve, cardthirteen, cardfourteen, cardfifteen, cardsixteen, cardseventeen, cardeighteen, cardnineteen, cardtwenty;

double ran, ranone, rantwo, ranthree, ranfour, ranfive, ransix, ranseven, raneight, rannine, ranten;

cardone = 1.01;
        cardtwo = 1.02;
        cardthree = 1.03;
        cardfour = 1.04;
        cardfive = 1.05;
        cardsix = 1.06;
        cardseven = 1.07;
        cardeight = 1.08;
        cardnine = 1.09;
        cardten = 1.1;
        cardeleven = 1.11;
        cardtwelve = 1.12;
        cardthirteen = 1.13;
        cardfourteen = 2.01;
        cardfifteen = 2.02;
        cardsixteen = 2.03;
        cardseventeen = 2.04;
        cardeighteen = 2.05;
        cardnineteen = 2.06;
        cardtwenty = 2.07;

So I want to randomly select ten of the doubles with the name "card" in them, eg. 所以我想随机选择其中十个双打,其名称为“card”,例如。 cardone, cardtwo, etc. cardone,cardtwo等

and I want the doubles named "ran" eg. 我想要名为“跑”的双打,例如。 "ranone" "rantwo" etc... to be set to be the same as the ten selected doubles. “ranone”“rantwo”等...被设置为与十个选定的双打相同。

I'd use a Collection of Double, say an ArrayList<Double> rather than an array of double, and then call Collections.shuffle(myArrayList) on the ArrayList. 我使用Collection of Double,比如说ArrayList<Double>而不是double数组,然后在ArrayList上调用Collections.shuffle(myArrayList) Bingo, randomized. 宾果,随机。 To get 10 of these values, simply call remove(0) on the list (after checking to make sure that the size is not 0). 要获得其中的10个值,只需在列表上调用remove(0) (在检查后确保大小不为0)。

This is not too different from shuffling a deck of cards and selecting 10 random cards. 这与洗牌和选择10张随机牌并没有太大区别。

Edit 编辑
I didn't realize that you were in fact trying to model playing cards. 我没有意识到你实际上是在试图模拟扑克牌。 If that is the case, then please stop what you're doing and forget using doubles. 如果是这种情况,那么请停止你正在做的事情并忘记使用双打。 Instead learn about and use enums since playing cards are almost the purest example of what enums are good for. 而是学习和使用枚举,因为扑克牌几乎是枚举有用的最纯粹的例子。 An excellent link that describes all of this can be found here . 可以在此处找到描述所有这些内容的优秀链接。

Here's a pretty easy solution: 这是一个非常简单的解决方案:

List<Double> doubles = new ArrayList<Double>();
// Add all 100 of your doubles
shuffle(doubles);

Then shuffle is generic and can work with any list: 然后shuffle是通用的,可以使用任何列表:

private static <T> void shuffle(List<T> list) {
    int size = list.size();
    Random rand = new Random();
    for (int i = 0; i < size) {
        list.add(list.remove(rand.nextInt(size - i)));
    }
}

This selects a random, not-yet-selected value and places it at the end of the list. 这将选择一个随机的,尚未选择的值,并将其放在列表的末尾。 If you want a better explanation, just say so. 如果你想要更好的解释,那就这么说吧。 The list will be randomized after the call to shuffle and you can just iterate through them to get the doubles. 在调用shuffle之后,列表将被随机化,你可以迭代它们来获得双打。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM