Thread: Java random from multiple ints

Results 1 to 4 of 4
  1. #1 Java random from multiple ints 
    Registered Member
    Join Date
    Apr 2012
    Posts
    214
    Thanks given
    16
    Thanks received
    8
    Rep Power
    13
    Just a quick one, how can I make java randomly select an int from this:



    Either the 1648, 1648 or 1612 option.

    Thanks.
    working on a 317.


    Reply With Quote  
     

  2. #2  
    #Winning

    Join Date
    Apr 2011
    Age
    28
    Posts
    2,245
    Thanks given
    367
    Thanks received
    388
    Rep Power
    1187
    Code:
    public int selectRandom() {
        switch(Misc.random(3)) {
            case 0:
            case 1:
                return 1648;
            break;
    
            case 2:
                return 1612;
            break;
        }
    }
    You could do the same thing with an if statement, but I'm sure you want to add more tasks in future. So this is a better option.


    Vouches
    Spoiler for Vouches:

    Quote Originally Posted by Exile rsps View Post
    Vouch thanks for that pic
    Quote Originally Posted by bwuk _im_pb View Post
    i vouch for charlie hes legit
    Quote Originally Posted by Pastaway
    Vouch for 'Charlie. Added a tut to my server I couldn't add and payed him after =]
    Quote Originally Posted by mongolias
    Vouch for 'Charlie. Sold me a banner, the trade went quick and efficiently.
    Quote Originally Posted by Lane
    Vouch for charlie, he is #1
    Quote Originally Posted by Raw Envy View Post
    Vouch for Charlie.
    Quote Originally Posted by -Null Code-
    Vouch Legit bought rs membership
    Quote Originally Posted by tucybro
    Traded with Charlie, went smoothly I went first and he paid promptly without and issues, great trader!



    Reply With Quote  
     

  3. Thankful user:


  4. #3  
    Registered Member
    Join Date
    Apr 2012
    Posts
    214
    Thanks given
    16
    Thanks received
    8
    Rep Power
    13
    Quote Originally Posted by Charlie View Post
    Code:
    public int selectRandom() {
        switch(Misc.random(3)) {
            case 0:
            case 1:
                return 1648;
            break;
    
            case 2:
                return 1612;
            break;
        }
    }
    You could do the same thing with an if statement, but I'm sure you want to add more tasks in future. So this is a better option.
    Thank you
    working on a 317.


    Reply With Quote  
     

  5. #4  
    Banned Java random from multiple ints Market Banned


    Join Date
    Jan 2011
    Age
    26
    Posts
    3,112
    Thanks given
    1,198
    Thanks received
    1,479
    Rep Power
    0
    Please use an enumerated type... here's your same code rewritten.

    Code:
    public enum SlayerTask {
    
    CRAWLING_HANDS(1648, 30, 70, 1, 30),
    CRAWLING_HANDS_2(1648, 15, 30, 40, 50),
    BANSHEE(1612, 25, 55, 20, 50);
    
    private final int npcId;
    private final int minimum;
    private final int maximum;
    private final int level;
    private final int experience;
    
    private SlayerTask(int npcId, int minimum, int maximum, int level, int experience) {
        this.npcId = npcId;
        this.minimum = minimum;
        this.maximum = maximum;
        this.level = level;
        this.experience = experience;
    }
    
    // getters here, not gonna bother typing them...
    
    }
    Then to get a random task...

    Code:
    SlayerTask t = SlayerTask.values()[(int)Math.random()*SlayerTask.values().length];
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. 317 Donor crown, loaded from new int
    By Shoes in forum Selling
    Replies: 32
    Last Post: 02-02-2011, 04:09 PM
  2. Replies: 2
    Last Post: 09-03-2010, 12:07 AM
  3. Java random number generator
    By Kenneh in forum Homework
    Replies: 16
    Last Post: 08-04-2010, 04:50 AM
  4. Replies: 1
    Last Post: 02-05-2010, 04:08 PM
  5. Replies: 12
    Last Post: 08-15-2007, 05:13 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •