Random Number

Discussion in 'Plugin Development' started by ryr11, Jan 10, 2014.

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

    ryr11

    I want to generate a random number between 1 & 97, then message the player:
    ChatColor.RED + "Your ID is" + ChatColor.GREEN + the random number
    How would I do that?
     
  2. Offline

    Jogy34

    Code:java
    1.  
    2. Random rnd = new Random();
    3.  
    4. int yourId = rnd.nextInt(97) + 1; //nextInt() will return a sudo-random number from 0 to # - 1
     
  3. Offline

    Jake6177

    I wouldn't depend on that if you're assigning IDs to players, as it's extremely likely to repeat itself. You'd need to make sure each player had a unique ID.
     
  4. Offline

    Maurdekye

    Why not just,
    Code:java
    1. int random = ((int) Math.random() * 97) + 1;
    2. player.sendMessage(ChatColor.RED + "Your ID is " + ChatColor.GREEN + Integer.toString(random))
     
  5. Offline

    _Filip

    Code:java
    1. UUID uuid1 = UUID.randomUUID();
    2. String uuids = uuid1.toString();
    3. int length = uuids.length();
    4. StringBuilder sb = new StringBuilder(uuids);
    5. for (int x = 0; x <= length/2; x++){
    6. length = sb.length();
    7. sb.deleteCharAt(sr.nextInt(length));
    8. sb.reverse();
    9. }
    10. String s = sb.toString();
    11. s = s.replaceAll("-", "");
    12. return s;
    13.  

    Is what I use if I want a REALLY random id. It's really resource intensive, but it's probably the most random string you're going to get.
     
  6. Offline

    DevRosemberg

  7. Offline

    ryr11

Thread Status:
Not open for further replies.

Share This Page