简体   繁体   中英

Printing random number values to an Array

Im a bit confused on how to do this particular process in Java.

I have to use a RNG to print a specific amount of values to an array, but the part I can't figure out is how to give each array element a value that is to be incremented if the RNG gives that value. Example:

Array

0
1 2

If the RNG returns a 2, then increment the 2 in the array, and then display it like this

0 1 2 1 (as in, it rolled a 2 once so its now 1)

I have no problems doing the user input and RNG part, but I don't know how to display it like that Any help would be appreciated, thanks.

Code so far

public static void main(String[] args) {

    Scanner input = new Scanner( System.in); //Declares the scanner
    Random randomNumbers = new Random(); // Declares the random property you will need later

    //Variables
    int randomrequest = 0; //Declares randomnum as a integer with a value of 0, this is what the user inputs as the number they want.
    int randomrange = 0; //Declares the number for the number range for the random number generator
    int randomcounter = 0;//Declares the counter you will need later as 0
    int result = 0; //This variable is for the random number generation result


    System.out.printf( "How many random numbers do you want to generate?" ); //asks for the number to generate

    randomrequest = input.nextInt(); //Makes the user enter a value to and then stores it in this variable.

    System.out.printf( "What is the number of values for each random draw?" ); //asks for the number range

    randomrange = input.nextInt(); //See above

    //Ok now we have to use the inputed information to do the rest of the processing before we can display it

    //First, create the array and give it the size equal to the number range entered

    int[] array = new int[ randomrange ]; // Declares the array with the amount of slots for each outcome

    //We need to use a loop here to make sure it rolls the RNG enough times 

    while (randomcounter != randomrequest) { //This tells it generate a random number until the counter equals the entered amount.

        result = randomNumbers.nextInt( randomrange ); //Generates a random number within the range given


        randomcounter += 1; //increments the counter, so that it will eventually equal the entered number, and stop.

    }//End of do while


    }//end of Public static void

}//End of entire class

If I'm interpreting your question correctly, one thing you can try is to have each element in the array be a counter for its index. So if your random number generator produces a 2, you increment the value in array[2] .

A concise way to put it might be:

while (randomCounter++ != randomRequest) {
    array[randomNumbers.nextInt(randomRange)]++;
}

The following code should work for your solution:

while (randomcounter != randomrequest) {
    result = randomNumbers.nextInt(randomrange);
    array[result] += 1;
    randomcounter +=1;
    for (int i = 0; i < array.length; i++)
    {
        system.out.print(array[i] + " ");
    }
    system.out.println();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM