mercredi 8 décembre 2021

Random function not giving an intended output

Random r = new Random();
int n = r.nextInt(101);

The output sometimes give an integer value greater than 100

    System.out.print("Value of n: " + n);
    int status = 1;
    int num=3;
    for(int i=2; i<=n; ){
        for(int j=2; j<=Math.sqrt(num); j++){
            if(num%j==0){
                status = 0;
                break;
            }
        }
        if(status!=0){
            System.out.println(num);
            i++;
        }
        status = 1;
        num++;
    }

can somebody explain the problem mentioned above?




Aucun commentaire:

Enregistrer un commentaire