Solved Random Code

Discussion in 'Plugin Development' started by Space_BR, Apr 16, 2014.

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

    Space_BR

    I want to make a simple random code:, if player type /generate (create a random code on config), and if player type /generate <code> (if its is = to the code who is on config give him a some diamonds).
    In the config is like this:
    --
    Code:
    Random:
    - 123456
    - 158714
    
     
  2. Offline

    Hoolean

    Should the code be of a specified length? Additionally, the Random class may be of interest.
     
  3. Offline

    Space_BR

    Hoolean
    6 characters of numbers
     
  4. Code:
    private Random random = new Random();
     
    public void generateNumber() {
        List<Integer> currentRandom = this.getConfig().contains("Random") ? this.getConfig().getIntegerList("Random") : new ArrayList<Integer>();
        Integer randomNumber = this.random.nextInt(999999);
        while (randomNumber < 100000) randomNumber = this.random.nextInt(999999);
        while (currentRandom.contains(randomNumber)) {
            randomNumber = this.random.nextInt(999999);
            while (randomNumber < 100000) randomNumber = this.random.nextInt(999999);
        }
        currentRandom.add(randomNumber);
        this.getConfig().set("Random", currentRandom);
        this.saveConfig();
    }
     
    public boolean playerWon() {
        return this.getConfig().getIntegerList("Random").contains(this.random.nextInt(999999));
    }
    
    You can change playerWon() if you like to make it have a minimum of 100000.
     
  5. Offline

    Space_BR

    KingFaris11
    What string can i show to code he generate, Like this: You get 652127 code use /generate 652127 to get some diamonds ?

    EDIT: Or can i use : currentRandom to show the code ?
     
  6. Offline

    Hoolean

    Here's the code I'd use to randomly generate a key (you may prefer KingFaris11 's version, just thought I'd throw mine into the equation also):
    Code:java
    1. private static final Random random = new Random();
    2.  
    3. public static String generateKey(int length)
    4. {
    5. return String.format("%0" + length + "d", random.nextInt(Math.pow(10, length)));
    6. }
     
  7. Offline

    Space_BR

  8. Offline

    Hoolean

    Space_BR

    Keep hold of the generated number separately and send that to the player; it looks like you are sending the entire list. :)
     
  9. Offline

    Space_BR

    Hoolean
    How can i make this? (i am noob, this is new for me)
     
  10. You're sending the whole List... you're meant to use list.get(index);

    Paste full code for that bit?

    No, currentRandom is the List of integers (random numbers), show them randomNumber.

    Edit: See below code.

    Code:
    private Random random = new Random();
    private List<Integer> randomList = null;
     
    public int generateNumber() {
        this.randomList = this.getConfig().contains("Random") ? this.getConfig().getIntegerList("Random") : new ArrayList<Integer>();
        Integer randomNumber = this.random.nextInt(999999);
        while (randomNumber < 100000) randomNumber = this.random.nextInt(999999);
        while (currentRandom.contains(randomNumber)) {
            randomNumber = this.random.nextInt(999999);
            while (randomNumber < 100000) randomNumber = this.random.nextInt(999999);
        }
        this.randomList.add(randomNumber);
        this.getConfig().set("Random", this.randomList);
        this.saveConfig();
        return randonNumber; // Returns the generated random number.
    }
     
    public int getWinningNumber() {
        if (this.randomList == null) this.randomList - this.getConfig().getIntegerList("Random");
        int randomNumber = this.random.nextInt(999999);
        return this.randomList.contains(randomNumber) ? randomNumber : -1; // This would return -1 if they did not win. By the way, you may want to add a minimum to this. (Under int randomNumber, while (randomNumber < x) randomNumber = this.random.nextInt(999999);
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  11. Offline

    IkBenHarm

    why do the while loop?
    you can also do: int i = random.nextInt(900000) + 100000;
    or i mistunderstand
     
  12. Offline

    Space_BR

    @Thanks
    Thanks for helping
     
Thread Status:
Not open for further replies.

Share This Page