mercredi 6 avril 2016

i have exceed number error in nextInt() of util.Random in Java 1.8.74 (Intellij Idea 2016.1.1)

I tried to get a random number between 3 and 6 (inclusive):

public class A {              

 Random ran = new Random();                            
 public func1(){            
  x = 3 + ran.nextInt(7); // need Random number x from 3 to 6   
  y = 3 + ran.nextInt(7)             
 }
}    

but it often gives results 8 and 9! How it can be ?

I used

 x=(int) (3 + Math.random()*6);  

its give 8 sometimes too...
where is an Error???




Aucun commentaire:

Enregistrer un commentaire