0

I am currently trying to edit the following code in java with static numbers to show a number in a range (this code is showing the distance from a certain point in blocks, and I need to make it difficult to find the block):

int R = getBlockTypeDistance(event.getClickedBlock().getLocation(), stid);
if (R == -1){
    player.sendMessage(ChatColor.BLUE + plugin.getConfig().getString("text.very_cold"));
} else if(R < 12) {
    player.sendMessage(ChatColor.RED + plugin.getConfig().getString("text.very_hot"));
} else if(R < 18){
    player.sendMessage(ChatColor.RED + plugin.getConfig().getString("text.hot"));
} else if(R < 27){
    player.sendMessage(ChatColor.GOLD + plugin.getConfig().getString("text.warm"));
}

I am looking for a randomly generated number for each else if statement with the following ranges:

Instead of R < 12, a range between 0 and 15

Instead of R < 18, a range between 15 and 22

Instead of R < 27, a range between 22 and 30

All ranges should have a "weight" (90% chance of getting a number higher than 75% in the range)

Any help appreciated, thanks.

mobile
  • 307
  • 1
  • 4
  • 15

2 Answers2

0

For the non-weighted cases, you can go random.nextInt(max-min+1) + min; And definiely check my math for an off by one error!

What do you mean by weighted?

It turns out that 0.9 ^ 13 is very close to 0.25 (if my math is correct - please double check!). So if you took a random in the range 0-1.0, raised to the 13th power, you'd get numbers less than 0.25 90% of the time. Throw in a 1-x to make it greater than 0.75 90% of the time.

user949300
  • 15,364
  • 7
  • 35
  • 66
  • He doesn't specify what he means by weighted which in my mind makes the question overly ambiguous and unanswerable (until improved). – Hovercraft Full Of Eels Dec 17 '13 at 03:26
  • I mean made so higher numbers appear more, as this is a distance thing, I would not want it to show 0-5 often for example, i cant just have a range of 5-15 because there would need to be something for 0-5 – mobile Dec 17 '13 at 03:29
  • 1
    This is just basic probability @mobile. There are lots of ways to do it, but you also need to tell us how much you want to weight it. Be a little more specific – Brant Unger Dec 17 '13 at 03:49
  • is a 90% chance of having more than 5 specific enough? – mobile Dec 17 '13 at 03:51
  • I've revised the original post to be more specific, my apologies. – mobile Dec 17 '13 at 04:00
  • See edited answer. @Brant Unger solution should also work, but mine would yield a smoother distribution. – user949300 Dec 17 '13 at 04:43
  • Being relatively new to java, how could I implement this in code? This wasn't my code originally. – mobile Dec 17 '13 at 05:05
  • This should work, but if you were doing a LOT of cases, this would yield a skewed distribution. Mine can scale to be more accurate with more cases based on the law of large numbers. So if you are only doing a small number of cases, use his. If you are running LOTS of cases, use mine. – Brant Unger Dec 19 '13 at 22:00
  • I should add...skewed...but not by much. – Brant Unger Dec 19 '13 at 22:02
0

Here's one way I could think to do it. Watch for comments to see if someone else has a better idea or improvements but this is what I came up with:

    private static int generateNumber(int min, int max)
    {
        Random rnd =  new Random();
        int randomInt;

        int weightedMin = (int)Math.round((max - min) * .75) + min; //Create a minimum number that is 75% of the way through the range

        //Generate a random number between 1 - 100
        //If that number is between 0 and 90 then generate the top end
        if ((rnd.nextInt(101) + 1) <= 90) randomInt = rnd.nextInt((max - weightedMin) + 1) + weightedMin; 
        else randomInt = rnd.nextInt((max - min) + 1) + min;

        return randomInt;
    }

Basically what it does is generates a random number between 1 - 100. If that number is between 1 and 90 inclusive it means it was in the 90% probability range, generate the 75%+ range. Otherwise it generates 75%-. Does this make sense?

Brant Unger
  • 403
  • 2
  • 11