0

i have a question about selecting items with specified rarity.

I have a Class "Card"

class Card {
    String name;
    int seltenheit;
    ArrayList<String> lore;
    String full_id;
    int id_int;
    byte id_byte;

    Card(String name, int seltenheit, ArrayList<String> lore, String id) {
        this.name = name;
        this.lore = lore;
        this.full_id = id;
        this.id_int = (id.contains(":")) ? Integer.parseInt(id.substring(0, id.indexOf(":")))
                : Integer.parseInt(id);
        this.id_byte = (id.contains(":"))
                ? Byte.parseByte(id.substring(id.indexOf(":") + 1, id.length()))
                : 0;
        this.seltenheit = seltenheit;
    }
}

and i've created a ArrayList with some Cards and every Card has it own "rarity" in this Class it is named "Seltenheit" because I am from Austria.

And i want to select 5 items from this ArrayList with the specified rarity. The rarity is a range between 1 and 100, 100 means it is very common and 1 means it is very rare and so on. So i need a function were it selects random 5 items with the rarity.

Sorry for my bad english :P I hope anyone can help me. Thanks.

Jonathan
  • 124
  • 9

2 Answers2

0
  1. Pick out all the cards with the selected rarity from your ArrayList into a separate collection.

  2. Select five random cards from your separate collection.

This general method will also work for cards with a range of rarity values if required.

You will need to deal with the case where there are less than five cards with that rarity. You might want to make the separate collection hold a reference to the card rather than duplicate the whole card.

rossum
  • 15,344
  • 1
  • 24
  • 38
  • Can you give me an example for Java? – Jonathan Jan 12 '19 at 09:42
  • I could, but you will learn more by doing it for yourself. Just scan through the card list, picking out those with the correct rarity. Then work with just the cards you have picked. – rossum Jan 12 '19 at 11:20
0

Possibly an old question: Randomly selecting an element from a weighted list

The answer is to sum all rarities together an put it in a binary search tree.

The you just need to select a node by random(1,sum_of_rarity).

(See the other answer for better explanation)

vikkre
  • 25
  • 4