dimanche 29 octobre 2017

Why is this Math.random() statement set out in this format?

I'm complete beginner to Java so I am sorry for what is probably a very stupid question. This is a programme for guessing two random numbers. Why is the Math.random followed by "* (MAX + 1 - MIN))) + MIN;". Also how does the Math.random know to generate a number between 1 and 3 and not anything higher?

  Scanner scan = new Scanner(System.in);
  final int MIN = 1, MAX = 3;
  int firstAnswer = ((int)(Math.random() * (MAX + 1 - MIN))) + MIN;
  int secondAnswer = ((int)(Math.random() * (MAX + 1 - MIN))) + MIN;
  int firstGuess, secondGuess;

Thanks for your help!!




Aucun commentaire:

Enregistrer un commentaire