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 7179131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:04:42+00:00 2026-05-28T17:04:42+00:00

I have an array that I populate with 6 randomly generated numbers. First it

  • 0

I have an array that I populate with 6 randomly generated numbers. First it generates a random number between 1 and 49 and then checks it against the numbers in the array. If it finds a duplicate it should generate a random number again and then perform the check once again. If there are no duplicates then the number is added to the array.

Here’s the code:

public void populateArray()
{
    for(int i = 0; i < numberLine.length; i++)
    {
        randomNumber = 1 + randomGen.nextInt(49);
        for(int j = 0; j < i; j++)
        {
            if (numberLine[j] == randomNumber)
            {
                i--;
            }
            else
            {
                continue;
            }
        }
        if(i >= 0)
        {
            numberLine[i] = randomNumber;
        }
        else
        {
            continue;
        }
    }
    Arrays.sort(numberLine);
}

However, for some reason this still lets in a duplicate, though rarely (about 1 in 50 arrays), such as 6 6 16 24 34 46. But when I try to duplicate this by taking out the random number element and using a number like 30, I am unable to reproduce the result. What’s going wrong?

  • 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-28T17:04:43+00:00Added an answer on May 28, 2026 at 5:04 pm

    Actually since your domain is limited to integers between 1 and 49 it’s better to use an array of booleans to indicate whether the number was already drawn:

    public void populateArray()
    {
        count = 0;
        boolean[] used = new boolean[50];
        while (count < 6) {
            randomNumber = 1 + randomGen.nextInt(49);
            if (!used[randomNumber]) ++count;
            used[randomNumber] = true;
        }
    
    
        int j = 0;
        for (int i = 1; i < used.length; ++i) {
            numberLine[j++] = i;
        }
    }
    

    edit

    That still has potential infinite loop.

    You’re drawing 6 numbers out of 49 without duplicates. The correct solution would be:

     public void populateArray() {
        List<Integer> pool = new ArrayList<Integer>();
        for (int i = 0; i < 49; ++i) {
            pool.add(i + 1);
        }
    
        for (int i = 0; i < 6; ++i) {
            randomNumber = randomGen.nextInt(pool.size());
            numberLine[i] = pool.get(randomNumber);
            pool.remove(randomNumber);
        }
    
        Arrays.sort(numberLine);
    }
    

    Finite loops, the same probability distribution as the original one. Instead of retrying the draw when a duplicate is encountered you just eliminate the possibility of drawing the duplicate beforehand. It’s basically emulating the real lotto-like draw.

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

Sidebar

Related Questions

I have an array that looks like $numbers = array('first', 'second', 'third'); I want
So I have an NSMutable array that I populate in viewDidLoad and then I
PROBLEM I have a nested PHP array that I need to populate from flat
I have a simple content edit form (Zend_Form) that I populate() with an array
if i have an array. can i populate a generic list from that array:
I have an associative array in awk that gets populated like this: chr_count[$3]++ When
I have an array that contains a list of vertices which I copy to
I have an array that looks like: $fields = array( f1 => array(test), f2
I have an array that has keys and values. For eg: Array ( [name]
I have an array that is a member of a structure: $self->{myArray} = [value1,

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.