Home > database >  Need to print maximum value for random number generator
Need to print maximum value for random number generator

Time:01-10

This is an assignment so please don't just give me the answer im trying to understand.

I need to use the command line to tell the program how many numbers to generate. Ex.

java RanNumGen 5
expected 94 16 12 56 21

The program does that i now need to have it print the maximun number generated which in the example is 94

Below is my code that give random numbers from 0 to 100 "n" number of times based off of command line input.

public class RanNumGen {
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        int max = 100;
        int min = 0;
        for (int i = 0; i < n; i  ) {
            int randomNumber = ((int)(Math.random() * (max - min)))   min;
            System.out.println(randomNumber);
        }
        
    }
}

I know that i need to establish Max And Min so that it knows what to print but i haven't been able to figure out how.

I have used int maxNum = Math.max(maxNum, randomNumber) but gotten the error cannot find symbol.
I understand that i would than use System.out.println(maxNum) to get the maximum number.

public class RanNumGen {
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        int max = 100;
        int min = 0;
        for (int i = 0; i < n; i  ) {
            int randomNumber = ((int)(Math.random() * (max - min)))   min;
            int maxNum = Math.max(maxNum, randomNumber);
            System.out.println(randomNumber);
            System.out.println(maxNum);
        }
        
    }
}

The code above gives error

RanNUmGen.java:8: error: ';' expected
                        int maxNum = Math.max(maxNum, randomNumber)
                                                                   ^
1 error
C:\Users\Steve\Desktop\TESU Coursework\COS-111-OL009\Intro CS\Programs>javac RanNumGen.java
RanNumGen.java:8: error: variable maxNum might not have been initialized
                        int maxNum = Math.max(maxNum, randomNumber);
                                              ^
1 error

CodePudding user response:

If I understand correctly you need to compute N random numbers and then find the max and the min between these?

If so, using the Math.max is the right approach but you first have to declare what maxNum is, otherwise the compiler will not find it. In particular you need a number either that is the first random number or the absolute minimum number that you'd come up with.

So either, get one random and generate N-1 more

int maxNum = ((int)(Math.random() * (max - min)))   min;
for (int i = 1; i < n; i  ) {

or initialize it before the loop

int maxNum = 0;

CodePudding user response:

The following sample code includes a method that returns an array of random unique integers

The parameters of the method allow to indicate the length of the array and the upper bound of the random unique integers

The main methods obtains an array of random unique integers, prints it, sorts it, and then prints the value of the greatest integer in the array

import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;

public class Main {

    // Returns an array of random unique integers 
    public static int[] getArrayOfRandomUniqueIntegers(int length, int bound) {
        HashSet<Integer> hashSet = new HashSet<Integer>();
        Random random = new Random();
        while (hashSet.size() < length) {
            int i = random.nextInt(bound);
            if (!hashSet.contains(i)) {
                hashSet.add(i);
            }
        }
        return hashSet.stream().mapToInt(Integer::intValue).toArray();
    }

    public static void main(String args[]) {
        int[] array = getArrayOfRandomUniqueIntegers(8, 75);
        System.out.println(Arrays.toString(array));
        Arrays.sort(array);
        System.out.println(array[array.length - 1]);
    }

}

CodePudding user response:

Your code below

    int maxNum = Math.max(maxNum, randomNumber);

passed a undeclared variable maxNum into the Math.max() function so that the error happened.

Although you have declared the variable as

    int maxNum 

, maxNum at the right side of the assignment "=" is still undeclared from the compiler's perspective. In detail, declaration "int maxNum" will only happen after the assignment "=" is done. So if you put the variable maxNum at the right side of the assignment (before it is declared), it will cause error because you try to assign a undeclared variable to another variable.

  •  Tags:  
  • Related