# Counting sort to random number generator.

Page 1 of 1

## 1 Replies - 185 Views - Last Post: 02 December 2017 - 12:00 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=407963&amp;s=7282a46de659add89ef4a7dab1a9d6f5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Falcun

Reputation: 0
• Posts: 1
• Joined: 01-December 17

# Counting sort to random number generator.

Posted 01 December 2017 - 11:21 PM

Made a random number generator and now I wanted to add counting sort to it but I'm very new to Java and honestly have no idea even where to start with that. The code I have below is just the generator. Any help will be appreciated. Thank you.

```package RandomNumberGen;

//Allow for keyboard input
import java.util.*;

public class RandomNumberGenerator{
//User Input
static Random rand = new Random();
static Scanner userInput = new Scanner(System.in);
//User input max/min values
static int maximum;
static int minimum;
//Number of randomly generated numbers
static int numberOfRandNum = 20;
//Sum of Random Numbers
static int sum;
//Random total
static double average;
//The string to represent the integers input
static String input;
//The user responses that are strings
static String restart;
static String display;
//Booleans
static boolean displayRandNums;

public static void main(String[] args)
throws InterruptedException {
//Start the process
startCode();
userInput.close();
}

public static void startCode()
throws InterruptedException {
/*
This method is the main body of the code. It takes
the user input and assigns it to the variables and interprets it.
*/

//User prompts
System.out.print("Enter a maximum number: ");
maximum = UserInputInt(maximum);
System.out.print("Enter a minimum number: ");
minimum = UserInputInt(minimum);
//Don't allow the minimum to be higher than the maximum
if(maximum <= minimum)
{
System.out.println("Error: maximum must be greater than minimum");
//Restart the process
startCode();
}
//User prompts cont.
System.out.print("How many random numbers do you want to average? ");
numberOfRandNum = UserInputInt(numberOfRandNum);
//Only allow non-negative array values
if(numberOfRandNum <= 0)
{
System.out.println("Please enter a number greater than zero");
startCode();
}
//User prompt for displaying every number generated
System.out.print("Would you like to see the random numbers?(Y/N) ");
display = userInput.next();
display = display.toLowerCase();
//Interprets response
if(display.equals("y") || display.equals("yes"))
{
//If the user wants to see the numbers, set the bool to true
displayRandNums = true;
}
else if(display.equals("n") || display.equals("no"))
{
//If the user doesn't want to see the numbers, set the bool to false
displayRandNums = false;
}
else
{
//If the user inputs an invalid input, tell them that, and restart
System.out.println("Input not understood.");
startCode();
}
//Collect the random numbers
getRandom();
endPrompts();

}

public static void getRandom()
{
/*
This method is designed to generate a random number
between the maximum and the minimum values input
by the user
*/
sum = 0;
//Create an array to store the values of the random numbers
int[] randomNumbers = new int[numberOfRandNum];
//Generate the random numbers
for(int x=0; x<numberOfRandNum; x++)
{
randomNumbers[x] = Math.abs(rand.nextInt() % (maximum - minimum +1)) + minimum;
sum = sum + randomNumbers[x];
//Average them
average = ((double) sum / (double) randomNumbers.length);
//Round answer to 3 decimal places
average = Math.round(average*1000)/1000.0;

//Allows the user to see the numbers that are randomly generated
if(displayRandNums)
System.out.println(randomNumbers[x]);
}
}

public static void endPrompts()
throws InterruptedException{
//Print average
System.out.println("The average is: " + average);
//Does the user want to repeat
System.out.print("Would you like to try again? (Y/N) ");
restart = userInput.next();
restart = restart.toLowerCase();
//Interpret user response
if(restart.equals("y") || restart.equals("yes"))
{
//User prompt to use the same numbers as the last attempt
System.out.print("Would you like to use the same numbers? (Y/N) ");
{
//If the user says yes, keep the values and try again
getRandom();
endPrompts();
}
{
//If the user wants to use different values, start the entire code over
startCode();
}
else
{
//If the user doesn't input one of the approved inputs, restart
System.out.println("Input not understood.");
startCode();
}
}
else if(restart.equals("n") || restart.equals("no"))
{
//If the user doesn't want to continue, display end message
System.out.println("Thanks for playing!");
System.exit(0);
}
else
{
//If the user doesn't input one of the approved inputs, exit
System.out.println("Input not understood. Goodbye!");
System.exit(0);
}

}

public static boolean numberOrNot(String input)
{
/*
This method is designed to check to make sure the user inputs an integer
instead of any other data type.
*/
try
{
//Try to convert the string to an int
Integer.parseInt(input);
}
//If it cannot convert the string to int, tell the program it cannot
catch(NumberFormatException ex)
{
return false;
}
//If it wasn't stopped by the error, tell the program it can convert it
return true;
}

public static int UserInputInt(int userInt)
{
/*
This method is designed to convert the user input to an integer if it
can, or tell the user to input an integer.
*/
//Have the user input a String
input = userInput.next();
//Call the numberOrNot method to check if the user input an integer
if(numberOrNot(input))
{
//If it can convert the string to an int, do it.
userInt = Integer.parseInt(input);
}
else
{
//If it cannot convert the string to an int, have the user input an integer
userInt = UserInputInt(userInt);
}
//Return the value of the new integer
return userInt;
}

}
```

Is This A Good Question/Topic? 0

## Replies To: Counting sort to random number generator.

### #2 ndc85430

• I think you'll find it's "Dr"

Reputation: 849
• Posts: 3,406
• Joined: 13-June 14

## Re: Counting sort to random number generator.

Posted 02 December 2017 - 12:00 AM

I'm not too sure what your question is. Do you know how the counting sort algorithm works? If you understand the steps, or have pseudocode for it, it shouldn't be too hard to implement.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }