简体   繁体   中英

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. (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?

EG Say double "one" is selected, along with 9 others.

I want to also set the double "blah" to the value of the selected double. I then want to set the double "blahtwo" to the value of the next selected double, etc...

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. cardone, cardtwo, etc.

and I want the doubles named "ran" eg. "ranone" "rantwo" etc... to be set to be the same as the ten selected doubles.

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. 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).

This is not too different from shuffling a deck of cards and selecting 10 random cards.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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