Built-in: Range between 2 nums – Scanner

Write a program, RangeBetween2_YI.java to prompt the user for two integers. One should be the min and the other one the max. The next prompt should ask for the quantity of random numbers to display within the specified range. Include a message at the start and at the end of the program.

NOTE: Do not use if or if/else statements.
Recommendation: look at these previous programs
SumOfTwoRand_YI.java for the random component where you simulate rolling two dices and then find the sum of the face value of the two of them.
ScannerTest_YI.java how to use the “for loop”.

 

/** 
* An itty bitty program to learn about Math.random()
* and a very useful tool: the for-loop
* GE
* 9/28
*
*/
public class MathRandTest_GE
{
   public static void main(String []args)
   {
      for (int i = 0; i < 50; i++)
         {
           System.out.println((int)(Math.random() * 6 ) + 1);
         }
    }
}
Let's understand Math.random()

Math.random() 0 ----> 0.99999999

((int)(Math.random() * 6 ) + 1)

Let's take any two random numbers from the random() function, 0.5 and 0.9

0.5 * 6 = 3.0
0.9 * 6 = 5.4

(Math.random() * 6 ) ---> 5.999… < 6 
(int)(Math.random() * 6 ) ---> 5
5 + 1 ----> 6

side note:
(int)6.999…. ----> 6 // the (int) truncates the decimal part.

 

Recall the Scanner class
/**
 *  good resource: http://docs.oracle.com/javase/tutorial/essential/io/scanning.html
 * @author gracielaelia
 * Problem: Both hasNext and next methods may block waiting for further input.
 * hasNext() doesn't return false ever!!!! next() blocks until something is input!
 * As you're reading from stdin, that'll either be when you send an EOF character (usually ^d on Unix), 
 * or at the end of the file if you use < style redirection.
 * You cannot easily send and EOF character using the keyboard"
 * -- usually Ctrl+D on Linux/Unix/Mac 
 * or Ctrl+Z on Windows does it
 */    

import java.util.Scanner;

public class ScannerTest_GE {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String name;  // declaration
        double base, exponent;
        System.out.println("Enter the base ");
        base = scan.nextDouble();
        System.out.println("Enter the exponent ");
        exponent = scan.nextDouble();
        System.out.println("Enter your name " );    
        name = scan.next();
        System.out.println(name + " the value is "+ Math.pow(base,exponent));
        System.out.println(name + " the integer value is "+ (int)Math.pow(base,exponent));
    }
}


// Enter the base 
// 2
// Enter the exponent 
// 5
// Enter your name 
// grace
// The value is 32.0
// The integer value is 32