Solved Selecting a random config key

Discussion in 'Plugin Development' started by GotChuNow, Dec 15, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    GotChuNow

    Basically I have a set of keys in a config, and I would like to randomly select one of them. The way I plan on going about doing this is counting how many keys there are in the config and selecting a random number from the amount of keys there are.

    This is what I have got so far, but I am not sure how I should proceed with the next part...
    Code:java
    1. int amount = pl.getCConfig("levels.yml").getKeys(false).size();
    2. int random = 1 + (int)(Math.random() * ((amount - 1) + 1));
    Any ideas?
     
  2. Offline

    turqmelon

    Code:java
    1. Set<String> keys = //code to get your keys
    2. int result = new Random().nextInt(keys.size());
    3. String key = (String) keys.toArray()[result];
     
    GotChuNow likes this.
  3. Offline

    GotChuNow

Thread Status:
Not open for further replies.

Share This Page