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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:20:41+00:00 2026-06-04T14:20:41+00:00

Just additional information: this is for a game project I’m working on. I’m trying

  • 0

Just additional information: this is for a game project I’m working on.

I’m trying to make an efficient randomization algorithm that places multiple number of objects (lined up with one another) given a length of how much is available space.

Straight to the problem: Given an available space of length = lengthOfArea, and number of objects to position = numObjects where each object’s length, lengthObject = (lengthOfArea/2) / numObjects. (having so that, the total amount of space that the objects occupy is half of the available space of length).

I’m supposed to look for the center positions of each object and return an array of it.

Here’s my current attempt:

public int[] FindObjectPositions(int lengthOfArea, int numObjects, Random random)
  {
    int lengthOfObject = (lengthOfArea / 2) / numObjects;
    int[] positionsPicked = new int[numObjects];
    int minPosition = lengthObject / 2;
    int maxPosition = lengthOfArea - (lengthObject / 2);
    for (int i = 0; i < numObjects; i++)
    {
      int newPosition;
      do 
        {
          newPosition = minPosition + random.Next(maxPosition - minPosition);
        } while (!IsPositionValid(positionsPicked, newPosition, lengthObject));
        positionsPicked[i] = newPosition;
    }
    return positionsPicked;
  }

public bool IsPositionValid(int[] positionsPicked, int newPosition, int lengthObject)
  {
    for (int i = 0; i < positionsPicked.Length; i++)
    {
      if (Math.Abs(positionsPicked[i] - newPosition) <= (lengthObject / 2))
        return false;
    }
    return true;
  }

Basically what I’m doing is I’m constantly randomly finding a valid position for each object. I’m just wondering if it’s inefficient. Also, I’m wondering if I’m ever going to reach a deadlock with this method?

  • 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-04T14:20:43+00:00Added an answer on June 4, 2026 at 2:20 pm

    It is possible to deadlock if there is a configuration where no more objects will fit. For instance, if there are 30 spaces and each object is 5-spaces long, the configuration

    _ _ _ _ x x x x x _ _ _ _ x x x x x _ _ _ _ x x x x x _ _ _
    

    would have no place that could possibly fit another object, even though there are 15 open spaces!


    Say there are m open spaces, and each object has width w. One method that wouldn’t deadlock would be to generate a list of m-w+1 numbers 1, 2, ..., m-w, m-w+1. Then choose one at random – this will represents the left-most position of your object. Then remove the w-1 numbers to the right and left of that number – this represents the fact that the object is w wide, and no other object of width w will fit within w-1 spaces to the left of it.

    For example, if m = 20 and w = 3,

    Create a list from 1 to 18

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

    Then choose one at random. Say we choose 10. Then spaces 10, 11, 12 are filled by our object, and spaces 8, 9 cannot be the leftmost position of another object, so we remove numbers 8 thru 12.

    1 2 3 4 5 6 7 13 14 15 16 17 18

    Say we choose 18 next. Then we remove 16-18:

    1 2 3 4 5 6 7 13 14 15

    And so on, until we have no numbers left.


    The above method will not deadlock, but it has another issue: it could be possible to have a configuration with (say) 6 objects in it, but the above method might only generate a configuration with 4 or 5 objects in it, and no room for any more. To solve this, rather than choosing random positions for our objects, we can figure out how many empty spaces there should be with 6 objects, and choose random spots for our empty spaces instead (using the above method).

    This will give you exactly the number of objects you want, randomly spaced, with no possibility of deadlock.

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

Sidebar

Related Questions

Just to make sure I'm understanding shallow copies of reference types correctly and that
I've just read about mode_t that it basically stores the following information: 7 boolean
I'm looking for a tutorial or any additional information on how to make an
ADDITIONAL INFORMATION 1 No errors are displaying in the console. Also this issue is
How can I add an additional icon to the Name/Lable of app, just the
Just wondering how I can make my app open automatically at login, but make
Just trying to get diff to work better for certain kinds of documents. With
Just a thing that annoys me. When I right click on a method name
I'm working on a small online game where there is a need to store
I would like to force a user to complete some additional information once they

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.