Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6133637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:15:17+00:00 2026-05-23T17:15:17+00:00

So I’m working on an OOP program that is meant to create 50 unique

  • 0

So I’m working on an OOP program that is meant to create 50 unique numbers, by using a random number generator that ensures no repeating numbers. I have the random part down and I’m using an extra method which swaps the numbers but I don’t know to only swap number if they are already used, I hope this is understandable.

import java.util.Random;
public class Project_1
{

    public static void main(String[] args)
    {
        Random rand = new Random();
        int a[][] = new int[11][6];
        for (int i = 1; i <= 10; i++) //row of values which fill in Student number
        {
            System.out.print("Student " + i + ":");
            System.out.print("\t");
            for (int j = 1; j <= 5; j++) //j is limited up to 5 columns
            {
                a[i][j] = 1 + rand.nextInt(50);
                CheckNumbers(a[i][j]); //input of the checkNumbers method
                System.out.print(a[i][j] + "\t"); // meaning that the numbers fill out according to table coordinates
            }
            System.out.println();
        }
    }

    public static void CheckNumbers(int[] x, int[] y)
    {
        int temp;
        for (int j = 0; j < 50; j++) //j must start at 1??? and we have 50 iterations, so countercontrolled to 50?
        {
            temp = x[i]; //this can be used to swap the numbers
            x[i] = y[i];
            y[i] = temp;
        }
    }
}

This is my program and if ran without the input of the CheckNumbers method my answer was

 ----jGRASP exec: java Project_1

Student 1:  8   35  8   5   40  
Student 2:  46  3   44  40  8   
Student 3:  27  47  28  11  3   
Student 4:  30  25  43  8   34  
Student 5:  3   12  45  6   5   
Student 6:  19  37  33  14  14  
Student 7:  9   31  6   39  32  
Student 8:  16  6   23  28  31  
Student 9:  19  34  49  42  11  
Student 10: 26  3   17  16  15  

 ----jGRASP: operation complete.

However as you see 8 is repeated (among may other numbers) so I put in the CheckNumbers method to stop it but I have an error filled compile message… so I am assuming that I have many errors in my CheckNumbers, so I need major help with that too.

Thank you all for your help!!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:15:18+00:00Added an answer on May 23, 2026 at 5:15 pm

    I’m not going to correct your methods. But if you want to create a random without repeating, create your own custom class.

    import java.util.BitSet;
    import java.util.Random;
    
    /**
     * Random number generator without repeat in the given range at construction time.
     * 
     * @author martijn
     */
    public class NoRepeatRandom {
    
        private Random random;
        private BitSet used;
        private int max;
    
        /**
         * Creates new instance of NoRepeatRandom, with the range <code>[0-max[</code>.
         * @param max the maximum for the range
         * @param seed the seed for the underlying {@link java.util.Random}
         */
        public NoRepeatRandom(int max, long seed)
        {
            this.max = max;
            this.used = new BitSet(max);
            this.random = new Random(seed);
        }
    
        /**
         * Creates new instance of NoRepeatRandom, with the range <code>[0-max[</code>.<br />
         * <code>System.currentTimeMillis()</code> is used as seed for the underlying {@link java.util.Random}
         * @param max the maximum for the range
         */
        public NoRepeatRandom(int max)
        {
            this(max, System.currentTimeMillis());
        }
    
        /**
         * Gives the next random number
         * @return a new random number. When finished it returns -1.
         */
        public int next()
        {
            if (isFinished())
            {
                return -1;
            }
            while (true)
            {
                int r = random.nextInt(max);
                if (!used.get(r))
                {
                    used.set(r);
                    return r;
                }
            }
        }
    
        /**
         * Tells if the random generator has finished. Which means that all number in the range
         * [0-max[ are used.
         * @return true if all numbers are used, otherwise false.
         */
        public boolean isFinished()
        {
            return max == used.cardinality();
        }
    
        /**
         * Sets all the numbers in the range [0-max[ to unused. Which means that all the numbers
         * can be reused.
         */
        public void reset()
        {
            used.clear();
        }
    
        /**
         * 
         * @return the maximum.
         */
        public int getMax()
        {
            return max;
        }
    }
    

    Usage:

    NoRepeatRandom myRandom = new NoRepeatRandom(50);
    for (int i = 0; i < myRandom.getMax(); ++i)
        System.out.println(myRandom.next());
    

    EDIT: JavaDoc added!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am using Paperclip to handle profile photo uploads in my app. They upload
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.