Array of Random Numbers

Write a program called Random50 that fills an array with fifty random values between 1 and 999. However, before a random value is entered into the array the program must check that the number generated is not already in the array. If it is already present then a new random number must be produced and similarly checked before entry. This process is repeated until a suitable number has been generated.

To create random numbers you must use an appropriate class from the Java library. The generation of random numbers has not been covered in the lecture course but you should be capable of researching this problem and locating the appropriate tool for this Exercise.

Once the array has been filled display the contents in the following format (i.e. ten lines of five numbers):
245 079 333 628 001
378 . . .

Notice that all the numbers are displayed as three digit number so you will need to use leading zeroes on numbers less than 100. There are a number of ways of doing this problem but your solution must conform to the following general requirements the outer loop is a while loop there is an inner loop to check for duplicates non-duplicate random numbers are stored and printed within the outer loop


i really need a hand just to show me how to start it off the basics please would be very greatfull : )

Comments
Not showing any effort, just want other people to do his homework for him.

Ok, I will help you with part of your research and will direct you to the Java 6 API, specifically to the java.util.Random class. This should help you with the creation of the Random numbers.
After creating the random numbers, think how you make sure that the number generated is unique. Try to work with that, and if you still have trouble with your code post it here and explain where exactly you get into trouble.

me and my m8 put this quickly we no its not for the question but we cant get it to not use the same numbers

import java.util.Random;
public class ArrayRandom{
	public static void main(String args[]){
		Random r = new Random();
		int arr[] = new int[20];

		for(int i = 0; i < 20; i++){
		   //random numbers from 1 to 10:
		   arr[i] = r.nextInt(10) + 1;
		}

		for(int i = 0; i < 20; i++){
		   System.out.print(arr[i] + " ");
		}
	} //main
} //class

Edited 6 Years Ago by shane1987: n/a

but it in the code brackets looks better lol : ) i dont no how to do it so we dont get the same numbers : )

Edited 6 Years Ago by shane1987: n/a

First, please post all of the information in one post, you can use the "edit" button next to your name. To give you an idea of what you need to do:

import java.util.Random;
public class ArrayRandom{
   public static void main(String args[])
   {
      Random r = new Random();
      int temp;
      int arr[] = new int[20];
      for(int i = 0; i < 20; i++)
      {
	 //random numbers from 1 to 10:
         temp = r.nextInt(10) + 1; // this will give you a random number between 0 to 10.
         
         //TODO: Here you need to check and see if any of the array cells 0 to i-1  
         //contain the same number as temp, and do a loop that exits only if the number is unique. 
         
         arr[i] = temp; //here we know that the number is unique, so we can put it inside arr[i]

      }
      for(int i = 0; i < 20; i++)
      {
	 System.out.print(arr[i] + " ");
      }
  } //main
} //class

hey I cant do this im stuck : (

I get random numbers but some are the same

alo all numbers need to be 3 numbers so like 879 and when i get 3 = 003

Edited 6 Years Ago by shane1987: n/a

Lets say you have a number stored in the variable called temp. How will you know if the variable temp2 holds the same value as temp?

sorry m8 i dont still dont understand what u mean i need to do something to stop me getting the same numbers and if i do it generates a new number

Ok, if you don't know how to do that, please read about the If-Then and the While loop. After you have read those you will have enough knowledge to answer the simple question I have asked you. Please show some effort.

im not that great at this m8 but thanx for you'r help i think ill just take the fail :(

oh sorry to bug you could you help me do 10 lines with 5 numbers on each because at the minute im just generating 50 numbers on 3 lines if you could help me with that please : )

zeroStr = "000" //string with 3 zeroes.

int r = String.valueOf(array[i]).length(); // get the length of element i

if (r < 3) { // for digits less than 3
     String temp = zeroStr.substring(0, 3 - r) + String.valueOf(array[i]); // concat the zeroes together with the digit

     System.out.println(temp);
}

In this way 3 will be printed as 003 (",);

is this right? sorry im new : (

import java.util.Random;
public class ArrayRandom
{
	public static void main(String args[])
		{
			Random r = new Random();
		
			int temp;
			int arr[] = new int[50];
			for(int i = 0; i < 50; i++)
                        {
                        //random numbers from 1 to 10:
                        temp = r.nextInt(1000); // this will give you a random number between 0 to 999
                        
                           
                          zeroStr = "000"  //string with 3 zeroes.
        
                          int r = String.valueOf(array[i]).length(); // get the length of element i

                          if (r < 3) {                                // for digits less than 3
 
                          String temp = zeroStr.substring(0, 3 - r) + String.valueOf(array[i]); // concat the zeroes together with the digit
        
                          System.out.println(temp);
   
                        }
                                            
                        arr[i] = temp;  //here we know that the number is unique, so we can put it inside arr[i]
                        }
                        for(int i = 0; i < 50; i++)
                        {
                               System.out.print(arr[i] + " ");
                        }
                }
                 
                
}

You did not compare temp to the other cells of the array in order to make sure that the number is unique. Please read again what I wrote to you here - you simply skipped that step.

Edited 6 Years Ago by apines: n/a

sorry guys you will have to bare with me as this is taking me long to pick up

I don't believe in completing assignments for students, but here's 2 clues for the generation of the numbers;

1.> Use the "Random" class to generate the numbers.
2.> Use one of the "Set" derivative classes to ensure all numbers are unique (e.g. "TreeSet<Integer>").

.> Use the "Random" class to generate the numbers.

i understand this bit

2.> Use one of the "Set" derivative classes to ensure all numbers are unique (e.g. "TreeSet<Integer>").

this is what im struggling withbig words as im dislexic is there any way of putting it simpler to me?

Edited 6 Years Ago by shane1987: n/a

i understand this bit


this is what im struggling withbig words as im dislexic is there any way of putting it simpler to me?

A "Set" in java stores a set of unique values. The best way to go about setting up your array is to create a "TreeSet<Integer>", and keep going in a loop adding your random numbers to it. If an existing number is added to the set, it won't increase in size because the existing value would be replaced; this means that you simply keep adding numbers in your loop until the set size equals 50.

import java.util.Random;

Public class ArrayRandom3
{
  public static void main(String[] args)
    {
        Random r = new Random();
        int array[] = new int[50];
        int a = 0;
        while (a < 50)
        {
          
            int random = r.nextInt(999);
            array[a] = random;

            // **********************
            //  make sure the numbers arnt the same here 
            //   
            // **********************
                                             
            
        a++;
        }
        int i = 0;
            for(int row = 0; row< 10; row++)
            {
            				for(int c = 0; c<=4; c++)
            				{
            				System.out.format("%03d ", array[i]);
            				i++;
            				}
            System.out.println("");
            }                        
        
    }
}

basicly all i need is to now check but the numbers in the array are not duplicated can anyone help me plz : )

import java.util.Random;

Public class ArrayRandom3
{
  public static void main(String[] args)
    {
        Random r = new Random();
        int array[] = new int[50];
        int a = 0;
        while (a < 50)
        {
          
            int random = r.nextInt(999);
            array[a] = random;

            // **********************
            //  make sure the numbers arnt the same here 
            //   
            // **********************
                                             
            
        a++;
        }
        int i = 0;
            for(int row = 0; row< 10; row++)
            {
            				for(int c = 0; c<=4; c++)
            				{
            				System.out.format("%03d ", array[i]);
            				i++;
            				}
            System.out.println("");
            }                        
        
    }
}

basicly all i need is to now check but the numbers in the array are not duplicated can anyone help me plz : )

Here's the code that you need...

import java.util.*;

public class RandomNumberPrinter
{
    public static final int ARRAY_SIZE = 50;
    public static final int MAX_NUMBER = 999;

    public static void main( String[] args )
    {
        LinkedHashSet<Integer> numberSet = new LinkedHashSet<Integer>();
        Random random = new Random();

        while( numberSet.size() < ARRAY_SIZE )
        {
            numberSet.add( random.nextInt( MAX_NUMBER + 1 ) );
        }

        Integer[] numbers = numberSet.toArray( new Integer[ 0 ] );

        for( int row = 0; row < 5; ++row )
        {
            for( int col = 0; col < 10; ++col )
            {
                int index = ( row * 10 ) + col;
                System.out.printf( "%03d ", numbers[ index ] );
            }
            System.out.println();
        }
    }
}

is there a site you can take me to asi need to read up on wat u just did as i need to do a psuedo code thanx for your input

because i dont understand this

LinkedHashSet<Integer> numberSet = new LinkedHashSet<Integer>();

Edited 6 Years Ago by shane1987: n/a

is there a site you can take me to asi need to read up on wat u just did as i need to do a psuedo code thanx for your input

I'm not sure of any specific sites, but the following classes are the important ones;

  • Random - This generates random numbers.
  • LinkedHashSet<T> - This class holds a set of unique values of type 'T'.

Google these class names to find out how they work. As for what the code is doing;

  1. Create a random number generator.
  2. Create an empty set to hold the unique values.
  3. Keep adding random numbers from 0-999 to the set until it's size is 50.
  4. Convert the set into an array.
  5. Print the array in a grid format with 5 rows and 10 columns.
This question has already been answered. Start a new discussion instead.