Solved Help with random

Discussion in 'Spigot Plugin Development' started by RayoxHD, Apr 19, 2017.

  1. Hello,

    i have this code to spawn an item in different chances but it doesnt work why ?

    Code (mainjava (Unknown Language)):
        @EventHandler
        public void onEntityDeath(EntityDeathEvent e) {
         
            if(e.getEntity() instanceof LivingEntity) {
                int num = Core.getRandom(1, 100);
                EffectsAPI.sendEffect(e.getEntity().getLocation(), EffectType.Spawner);
                e.getDrops().clear();
                e.setDroppedExp(0);
                //Debug
                e.getEntity().getKiller().sendMessage(""+num);
                         
                if(num < 0 && num >= 7) {
                    e.getDrops().add(Core.getGems(Core.getRandom(1, 2)));
                }else if(num < 7 && num >= 20) {
                    e.getDrops().add(Core.getDiamond(Core.getRandom(1, 4)));
                }else if(num < 25 && num >= 50) {
                    e.getDrops().add(Core.getRedstone(Core.getRandom(1, 8)));
                }else if(num < 50 && num >= 100) {
                    e.getDrops().add(Core.getGold(Core.getRandom(1, 12)));
                }
             
            }
    Code (corejava (Unknown Language)):
        public static int getRandom(int min, int max) {
            SecureRandom srnd = new SecureRandom();
            int num = srnd.nextInt(max) + min;
           
            return num;
        }
     
  2. Your random should be srnd.nextInt(max - min) + min; But not sure if that's what's wrong?
     
  3. nah its not that its debug the num 89 but gives me the item for > 7
     
  4. Take a look at your if-statements and the order they're processed in. if x is equal to 50, and you need to give the highest item, in the checks BEFORE the highest check, make sure you also check if the number is less than that amount. That was vague but you might understand what I mean.
     
  5. Why are you getting into cryptography? SecureRandom is only for KeyPairGenerator instances. Just use Random and SecureRandom has a tendancy to give out 0xffffff or something like that.
     
    • Agree Agree x 1
  6. Oh sry my bad. Thx a lot
     
  7. Do check on the if-statements though.
    See the mistake? A number that's both smaller than 0 AND equal/bigger to/than 7 is quite a strange situation.
     
    • Agree Agree x 1
  8. ahh now i see it got it twisted in my head! thanks
     
  9. Like when you prove the pi is an algebraic number by saying there is a whole number between 0 and 1. :cool:
     
    • Agree Agree x 1