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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:57:40+00:00 2026-06-04T17:57:40+00:00

Here is some simple code to show my problem: void method() { for(int i

  • 0

Here is some simple code to show my problem:

 void method()
 {
   for(int i = 0;i<=99)
   {
     method1();
     method2();
   }
  }

void method1()
{
    if(Randombool())
    {
        bool exists = true;
        int n;
        while(exists)
        {
            n=RandNum(100);
            exists = list1.Exists(num => num == n);
        }
        list1.add(n);
    }
}

void method2()
{
    int n;
    bool exists = true;
    bool exists2 = true;
    while(!(exists && !exists2))
    {
        n = RandNum(100);
        exists = list1.Exists(elem => elem == n);
        exists2 = list2.Exists(elem => elem == n);
    }
    list2.add(n)
}

Well it’s obvious it will be stuck in the while loop from method2 for a long time.

Is there a more gentle method to generate numbers so I can avoid waiting?

  • 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-06-04T17:57:41+00:00Added an answer on June 4, 2026 at 5:57 pm

    It seems like you’re trying to generate a random sequence of N numbers. The approach you’re using here is to get a random number and then discard it if it is one you already have.

    What you want to do is Shuffle an Array which can be done reasonably efficiently. Just populate the list with 0, 1, 2, … in order and then shuffle it.

    Pseudocode copied from the above link:

    To shuffle an array a of n elements (indices 0..n-1):
      for i from n − 1 downto 1 do
           j ← random integer with 0 ≤ j ≤ i
           exchange a[j] and a[i]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a quite strange problem here. I'm calling some simple code via Ajax.Updater:
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
So, here's some sample javascript code: Object.prototype.simpleFunction = function () { return true; }
Here is a simple code I have problem with: <label id=label1> <?php echo Text
I got some sample code from the net here: http://www.javadb.com/sending-a-post-request-with-parameters-from-a-java-class That works fine. It
This is similar to (but different from) this question . Here is some simple
How do you make a grid width 100% inside a canvas? Here's some simple
Here's some very simple sample HTML for purposes of this question: <!DOCTYPE html> <html>
What am I doing wrong here: class Helo { // main: generate some simple
Have a simple form (only extract fields here) but for some reason the JQserilization

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.