Solved Random() isn't functioning

Discussion in 'Plugin Development' started by DatCookiez, Jan 16, 2015.

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

    DatCookiez

    Hey guys, I have a Random() and it's just not working for this... the code fires all the time

    Code:
    Random rand = new Random();
    int hit = rand.nextInt(100);
    
    [code=java]  
        @EventHandler
        public void onMageHit(EntityDamageByEntityEvent e){
          
                if (e.getDamager() instanceof Player && e.getEntity() instanceof Player){
                    Player p = (Player) e.getDamager();
                    Player v = (Player) e.getEntity();
              
                    Location loc = v.getLocation();
                    Vector vec = loc.getDirection().setY(0);
          
                        if (Main.mage.contains(v)){
                            if (hit <= 50){
    
                            p.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 20*2, 1));
                            p.sendMessage(ChatColor.DARK_PURPLE + "Ouch! Mages aura poisons you!");
                            ParticleEffect.SMOKE_LARGE.display(vec, 0, loc, 50);
              
                    }
                }
    
            }
        }
     
    Last edited: Jan 16, 2015
  2. Offline

    API_Tutorials

    @DatCookiez
    You only use the random once, which means the value will always be the same.



    Code:
    if (Main.hit <= 50)
    ->
    Code:
    if (rand.nextInt(100) <= 50)
     
  3. Offline

    Dsi_Mario

    Make a runnable and put this line of code into it:
    Code:
    rand =newRandom();
    Please do note that this can be resource intensive. Another alternative would be to reference Math.random() instead of the integer rand.
     
  4. Offline

    SuperOriginal

    @Dsi_Mario Why would you put it in a runnable.....
     
    Konato_K likes this.
  5. Offline

    Dsi_Mario

    Tis true...
     
  6. Offline

    PreFiXAUT

    @DatCookiez simply init hit again in the onMageHit()
     
  7. Offline

    Josh014

    Try to put your random method in your event. Because the random method needs to be triggered before it can generate a random integer.
     
  8. Offline

    xTrollxDudex

    You're kidding right?
     
Thread Status:
Not open for further replies.

Share This Page