1

So I am doing a random number generator where the user inputs lowest value, highest value and desired amount of random numbers they want to receive out of a range and was trying to add a sentence in the beginning of every number out put it gives me.I have worked my way up to where it generates me the 'x' amount of numbers when given lowest and highest value. It prints out the results one by one in a new line and I was wondering if I could add a string that loops with the amount of times the user wants to generate random numbers from the range.

//This java utility was used to allow the program to choose random number.
import java.util.Random;
//This java utility was used to allow the program to allow the user to input requested data.
import java.util.Scanner;
import java.util.function.IntConsumer;

class Main {

    public static void main(String[] args) {

        /*
         * A welcoming message was added to give the user information on what the
         * program was created for. The program asks for the user to choose a range and
         * to input the lowest number in the range, highest number in range, and how
         * many results should the program print
         */

        System.out.println("Hello, this program will compute a set");
        System.out.println("amount of random numbers in the given range.");
        System.out.println("");

        /*
         * This set of code asks the user to give the value of the lowest and highest
         * integer in their range and translates the string into an integer using
         * Integer.parseInt(lower/higherLimit). This allows the computer to understand
         * the actual value of the number given. It then takes the value added and puts
         * in into the variables of 'min' and 'max' for the lowest and highest numbers
         * given respectively.
         */

        Scanner input = new Scanner(System.in);
        System.out.print("Enter lower limit of the range:");
        String lowerLimit = input.nextLine();
        int min = Integer.parseInt(lowerLimit);

        Scanner input1 = new Scanner(System.in);
        System.out.print("Enter higher limit of the range:");
        String higherLimit = input.nextLine();
        int max = Integer.parseInt(higherLimit);

        /*
         * 
         */
        Scanner input3 = new Scanner(System.in);
        System.out.print("How many numbers shall I print:");
        String amountPrinted = input3.nextLine();
        int amount = Integer.parseInt(amountPrinted);

        int amount1 = amount;
        int min1 = min;
        int max1 = max;
        {
            Random random = new Random();

            random.ints(amount1, min1, max1).sorted().forEach(System.out::println);


        }

    }
}
GBlodgett
  • 12,704
  • 4
  • 31
  • 45

1 Answers1

1

I think you have to use a lambda expression for this. A simple method reference won't quite handle it.

   public static void main( String[] args ) {
      int amount1 = 10;
      int min1 = 3;
      int max1 = 18;
      Random random = new Random();
      random.ints( amount1, min1, max1 ).sorted()
         .forEach( i -> System.out.println( "Your number is " + i ) );
   }

There are a few ways to add an index (counter) to this. Here's a simple one, but it kinda abuses the Random class.

  IntStream.range( 0, amount1 ).forEach( i -> 
      System.out.println( "Rng #" + i + " is " + 
           (new Random().nextInt( max1-min1 ) + min1 )) );

You can also add counters with something like AtomicInteger. See this question and answer:

Is there a concise way to iterate over a stream with indices in Java 8?

markspace
  • 10,621
  • 3
  • 25
  • 39