Random Numbers?

Discussion in 'Plugin Development' started by IcyRelic, Jun 15, 2012.

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

    IcyRelic

    How do i generate a random number between ## and ##?
     
  2. Offline

    desht

    http://docs.oracle.com/javase/6/docs/api/java/util/Random.html

    Assuming you want an integer:
    PHP:
    // returns random value between lower and upper inclusive
    // assumes lower is actually smaller than upper!
    public int getRandom(int lowerint upper) {
        return new 
    Random.nextInt((upper lower) + 1) + lower;
    }
     
    gidonyou likes this.
  3. Offline

    IcyRelic

    do i need to make lower and upper equal something before this
    like this

    Code:
    int lower = 0;
    int upper = 1;
     
    public int getRandom(int lower, int upper) {
        return new Random.nextInt((upper - lower) + 1) + lower;
    }
     
  4. Offline

    Dino Filippini

    Something like that. What Desht posted was a function, with upper and lower being the parameters. You would type

    getRandom(0,5);

    to return a random value between 0 and 5 (including 5). This is merely an example but you get the idea.
     
  5. Offline

    IcyRelic

    Random.nextInt cannot be resolved to a type
     
  6. Offline

    CRAZYxMUNK3Y

    What about something like this;
    Code:java
    1.  
    2. int upper = 5;
    3. Random random = new Random();
    4. random.nextInt(upper)
    5.  

    Return a value between 0 and 5; or you can do;
    Code:java
    1.  
    2. int upper = 5;
    3. Random random = new Random();
    4. random.nextInt(5+(upper))
    5.  


    Return a value between 5 and 10
     
    Profaann likes this.
  7. Offline

    Dino Filippini

    rather:

    int x = getRandom(0,5);

    His function should return an integer, so the above should work, no?
     
  8. Offline

    IcyRelic

    Would this work

    Code:
    @EventHandler
        public void onplayerjoin(PlayerJoinEvent event){
            Player p = event.getPlayer();
           
            int upper = 5;
     
            Random random = new Random();
     
            p.sendMessage("Random Number: " + random.nextInt(5+(upper)));
        }
    if so how do i get a random number between 1,500,000 and 10,000,000

    i cant test that because the function gives an error in Eclipse

    got it working but how do i get it to add the commas

    Code:
    package me.icyrelic.com;
     
    import java.util.Random;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
     
    public class PlayerJoin implements Listener {
       
        OutOnBail plugin;
        public PlayerJoin(OutOnBail instance) {
     
            plugin = instance;
     
            }
       
        @EventHandler
        public void onplayerjoin(PlayerJoinEvent event){
            Player p = event.getPlayer();
           
     
            p.sendMessage("Random Number getRandom: " + getRandom(1500000,10000000));
        }
       
        public int getRandom(int lower, int upper) {
            Random random = new Random();
            return random.nextInt((upper - lower) + 1) + lower;
        }
           
     
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  9. Offline

    desht

    What commas? The code you posted above looks OK to me.
     
  10. Offline

    IcyRelic

    it sends this

    Random Number: 1000000
    i want it to send
    Random Number: 1,000,000
     
  11. Offline

    desht

    I would suggest typing "java commas in numbers" into Google. There are a ton of answers and examples.
     
  12. Offline

    IcyRelic

    found this
    Code:
    private String insertCommas(String str)
        {
            if(str.length() < 4){
                return str;
            }
            return insertCommas(str.substring(0, str.length() - 3)) + "," + str.substring(str.length() - 3, str.length());
        }
    but the number isnt a string and i cant do .toString() on it so how do i make it a string

    i got it working
    Code:
    package me.icyrelic.com;
     
    import java.util.Random;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
     
    public class PlayerJoin implements Listener {
       
        OutOnBail plugin;
        public PlayerJoin(OutOnBail instance) {
     
            plugin = instance;
     
            }
       
        @EventHandler
        public void onplayerjoin(PlayerJoinEvent event){
            Player p = event.getPlayer();
            int num = getRandom(1500000,10000000);
            plugin.getServer().broadcastMessage("Judge: " + p.getName() + "'s Bail Set To: " + insertCommas(convertInteger(num)));
           
           
           
           
        }
       
        public int getRandom(int lower, int upper) {
            Random random = new Random();
            return random.nextInt((upper - lower) + 1) + lower;
        }
       
        private String insertCommas(String str)
        {
            if(str.length() < 4){
                return str;
            }
            return insertCommas(str.substring(0, str.length() - 3)) + "," + str.substring(str.length() - 3, str.length());
        }
       
        public static String convertInteger(int i) {
            return Integer.toString(i);
        }
           
     
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  13. I recommend doing this to format the number:
    Code:java
    1.  
    2. public String format(int number)
    3. { DecimalFormat formatter = new DecimalFormat("#,###,###");
    4. return formatter.format(number);
    5. }
     
Thread Status:
Not open for further replies.

Share This Page