Hello! Tell me by the code, it is correctly designed by the task.

Task.

Create a program that will check whether an integer randomly selected from the [5; 155] segment is in the interval (35; 150) and report the result to the screen

public class Test { public static void main(String args[]){ final int a = 5; final int b = 155; int c = (int) (Math.random()*a + Math.random()*b); if(c >= 35 && c <= 150){ System.out.println("Число " + c + " содержится в интервале (35,150)"); } else{ System.out.println("Число " + c + " не содержится в интервале (35,150)"); } } } 
  • Thanks for the answer "Nofate" - turtles

1 answer 1

Do you think that a number in the interval [0; 5] plus a number in the interval [0; 155] will give a number from the interval [5; 155]?

To get a random number from the interval [a; b] use the formula:

 c = Math.random()*(b - a) + a 

Checking:

 [0; 1]*(b - a) + a = [0; b - a] + a = [a; b - a + a] = [a; b]