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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:50:11+00:00 2026-05-25T22:50:11+00:00

I want to generate random numbers within a range and the generated numbers should

  • 0

I want to generate random numbers within a range and the generated numbers should not collide within some range.
i used following code but im getting Stackoverflow error.. Any better solution?

static int [] xPositions=new int[10];
int WIDTH=700
public static void main(String args[])throws IOException
{

    if(generateRandomXPositions(10)){
        for(int i=0;i<10;i++){
          System.out.println(" Random Numbers "+i+"  :"+xPositions[i]);
        }
    }


}

private static boolean generateRandomXPositions(int n) {

    for(int i=0;i<10;i++){
        int temp=((int)0 + (int)(Math.random() * ((WIDTH - 0) + 1)));
        for(int j=0;j<xPositions.length;j++){
            if(xPositions[j]>temp-50 && xPositions[j]<temp+50){ // IF GENERATED NUMBER IS IN THIS RANGE IT SHOULD REGENERATE THE NUMBERS 
                generateRandomXPositions(10); 
            }
        }
        xPositions[i]=temp;
    }
    return true;
}

I know problem is here

if(xPositions[j]>temp-50 && xPositions[j]<temp+50).   

Below one works fine

`if(xPositions[j]==temp)`.  

But I need that random numbers should follow that range! .

Many are wondering about the exit condition of recursive loop. But I believe if random number is not in that range, then there is no point of entering in to the recursive loop.

UPDATE 1:

And I believe compiler is tired to find the number between this range! Now I found that it is impossible fit 10 images having width of 100px each in to the 700px width container without colliding X positions!

Please see the image below. Lets imagine i want to place this boxes randomly without colliding… how can i do that?

enter image description here

  • 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-25T22:50:11+00:00Added an answer on May 25, 2026 at 10:50 pm

    You’re getting a StackOverflowError because the chances of xPositions[j]>temp-50 && xPositions[j]<temp+50 not passing is very low when there’s a range of 50. The chances of this function terminating is even lower due to the inner for-loop. Thus, this will keep recursing..

    However, it doesn’t seem like you’re doing what you actually want to accomplish. If you want to generate numbers that are all within a set range, you don’t want to compare xPositions[j] to temp-50 and temp+50. That’s going to regenerate numbers when xPositions[j] isn’t within some random range.


    If you really just want to generate numbers that are within a certain range, then you’ll want to get rid of the inner for-loop and instead do something like this:

    for every number in xPositions:
      a = random number
      if a is within an unwanted range, regenerate numbers
      else set xPositions[i] = a
    

    or without recursion:

    for every number in xPositions:
      a = random number
      while a is within an unwanted range:
        a = random number
      set xPositions[i] = a
    

    On the other hand, if you want to randomize the order of some images along the x-axis, you can do something like this:

    bag = [0 1 2 ... n-1]
    shuffle bag
    for every number in xPositions:
      xPositions[i] = bag.pop * IMAGE_WIDTH
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to generate random numbers, but these numbers should be somewhat biased, so
How can I generate a random number within a range but exclude some, without
I want to generate random numbers with a range (n to m, eg 100
i want to generate a sequence of unique random numbers in the range of
i want to generate an array of random numbers for example if the range
I need to generate random UNIQUE numbers within a range, how can I do
Possible Duplicate: Generate Random numbers uniformly over entire range I want to generate the
I want to generate (pseudo) random numbers between 0 and some integer. I don't
I want to generate random numbers manually. I know that every language have the
I want to generate random numbers from -n to n excluding 0. Can someone

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.