Hello guys,

I need help with one task. I've to make a program which generates random numbers and stores them in an array. How many numbers to be extracted depends on the arrays length, which are parameters in the class constructor. The array needs to contain only one instence of each number, in a range from 100 to 1000, both limits included. I also need to find the smallest, largest and the average value of the numbers in the array and the absolute value closest to the average value. At last I need to print my result on a text area with JOptionPane.showMessageDialog.
When I've finished this class (UniqueNumber), I need to make a new class with my main-method, and test out all methods on UniqueNumber class.

How should I begin on this program? Could anyone give me some hints/tips for how I should start? I guess I need to declare some private fields for the Random generator, the array (private int[] randNum and private Random generator... Do I need something more?) and constructor for these fields.

Recommended Answers

All 13 Replies

Sounds like you're heading in the right direction. A class that creates the (private) array, with methods for min/max etc. A testing class with a main method that creates an instance of the number class and calls its methods to get values that it displays.
I think you've got enough to start coding. Start with the smallest subset that you can possibly execute (eg main creates an instance that just populates its array with numbers 1..n and prints them to prove its working.
Then do random numbers, then the statistical methods. Finally do the GUI display. At every stage compile and test the code (using simple print statements) to get things working one bit at a time. Do not attempt to code it all in one go.

Thanks for your help. One of my problems is that I clearly don't understand how I can generate all those numbers in range 100 to 1000, and that the array only need to consist of one instaece of each number..

Member Avatar

a. Look up the Math class
b. Keep an array of booleans, indexed 0-900, and keep track of when a particular number is generated. ie. If 123 is generated, 123 - 100 = 23 (your index of your array). If it already set to true, disregard and generate another number.

Another idea on how to achieve uniqueness: Do it like cards in a deck: Generate the numbers 100 to 1000 and put them, in order, in an array of 901 elements (0 to 900). Then "shuffle" them: For each element, from 0 to 900, swap that element with a randomly selected index/element. Then, given that they want "N" numbers, use only the first "N". Doing this avoids performance problems that occur when the user asks for near 900 elements (or over 901!!!).

Thanks for help. I could make an array with 900 elemenets, but my task says that my arrays should have something between 50-100 elements. It all depends on the arrays length...

Maybe you just have to do it the hard way - for each new random number check if its already in the array by scanning through the existing entries - not elegant, but surprisingly fast.

Hello guys, and thanks for your answers. I managed to solve my problem by creating a boolean method which runs through the array and check if it has the same number. If it has, it will return true, and if not, false. Then I created another class which generates the numbers using the boolean class.

I have a few more questions though. I need to find the minimum, maximum and the average value, and I have no idea for how I should do that. I thought about using some if-else statements, but I don't do it right..

I have these under my class:
private final int MAX_VALUE = 1000, MIN_VALUE = 100; //(i need to use these)

//If I can find the minimun vaue, I think I'm able to find the max value. 

 public int lowestNumber()
       int low = MIN_VALUE;
	if( min >= MIN_VALUE && min < MAX_VALUE ) // i declared min as private statement

	  low += min;

return low;

I don't get the output I want, just 100. What am I doing wrong? I fould be thankfull if somoene could help me to find the average number and the absolute value closest to the average :D

Hmm... OK, where does "min" variable come from in this case?

To find a minimum value in a collection such as an array, you need to set a variable with the greatest possible value. Then you iterate through the collection/array and compare the value from the collection with the variable you have. If the value from the collection is lower than the value you have, set the value from the collection to the value. After you finish the iteration, the value should contain the lowest number.

commented: Good short answer that sets the student in the right direction. +6

I declared is as a private statement.. But I changed it a little bit: Do you mean something like this?

public int lowestNumber()
     int min = MAX_VALUE;
     for(int i = 0; i<randNum.length; i++)
	  if(randNum[i] < min)
	        min = randNum[i];

     return min;


Hmm, I don't know why, but I managed to solve my problem by setting int i = 1 in the for-loop.. Could anyone explain what happened? xD why didnt 0 work, but 1?

What is the problem when you use int i=0? Does it throw an exception? What kind of the error??? Did you check what your randNum array contains? You could simply display it on the monitor to see if there is any unexpected value in your array.

I'm guessing that you didn't initialize randNum[0], so it's value was zero. If your random numbers are never less than 100, this would make it seem as if it's not working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.