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

  • Home
  • SEARCH
  • 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 791507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:50:52+00:00 2026-05-14T21:50:52+00:00

The task is this: generate k distinct positive numbers less than n without duplication.

  • 0

The task is this: generate k distinct positive numbers less than n without duplication.

My method is the following.

First create array size of k where we should write these numbers:

int a[] = new int[k];

//Now I am going to create another array where I check if (at given number
//position is 1 then generate number again, else put this number in an
//array and continue cycle.

I put a piece here of code and explanations.

int a[]=new int[k];
int t[]=new int[n+1];
Random r=new Random();
for (int i==0;i<t.length;i++){
    t[i]=0;//initialize it to zero
}

int m=0;//initialize it also
for (int i=0;i<a.length;i++){
    m=r.nextInt(n);//random element between  0 and  n
    if (t[m]==1){
        //I have problems with this. I want in case of duplication element
        //occurs repeat this steps afain until there will be different number.
    else{
        t[m]=1;
        x[i]=m;
    }
}

So I fill concret my problem: if t[m]==1. It means that this element occurs already so I want to
generate a new number, but problem is that number of generated numbers will
not be k because if i==0 and occurs duplicate element and we write continue then it will switch at i==1.
I need like goto for the repeat step. Or:

for (int i=0;i<x.length;i++){
    loop:
        m=r.nextInt(n);

    if ( x[m]==1){
        continue loop;
    }
    else{
        x[m]=1;
        a[i]=m;
        continue; //Continue next step at i=1 and so on.
    }
}

I need this code in Java.

  • 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-14T21:50:53+00:00Added an answer on May 14, 2026 at 9:50 pm

    It seems that you need a random sampling algorithm. You want to be able to choose m random items from the set {0,1,2,3…,n-1}.

    See this post, where I wrote about 5 efficient algorithms for random sampling.

    Following is Floyd’s implementation, which can be useful in your case:

    private static Random rnd = new Random();
    ...
    public static Set<Integer> randomSample(int n, int m){
        HashSet<Integer> res = new HashSet<Integer>(m);
        for(int i = n - m; i < n; i++){
            int item = rnd.nextInt(i + 1);
            if (res.contains(item))
                res.add(i);
            else
                res.add(item); 
        }
        return res;
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.