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 lower, int upper) { return new Random.nextInt((upper - lower) + 1) + lower;}
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; }
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.
What about something like this; Code:java int upper = 5;Random random = new Random();random.nextInt(upper) Return a value between 0 and 5; or you can do; Code:java int upper = 5;Random random = new Random();random.nextInt(5+(upper)) Return a value between 5 and 10
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.
I would suggest typing "java commas in numbers" into Google. There are a ton of answers and examples.
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.
I recommend doing this to format the number: Code:java public String format(int number){ DecimalFormat formatter = new DecimalFormat("#,###,###");return formatter.format(number);}