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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:19:45+00:00 2026-06-13T13:19:45+00:00

This is what I have in my method to randomly select a element in

  • 0

This is what I have in my method to randomly select a element in my array, however I’m not sure why it isn’t working, I feel like I have tried every way of writing it, any ideas.

public static Seat BookSeat(Seat[][] x){

  Seat[][] book = new Seat[12][23];

    if (x != null){  

      book = x[(Math.random()*x.length)];  

    }

  return book;    
}
  • 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-13T13:19:47+00:00Added an answer on June 13, 2026 at 1:19 pm

    The way you explain things makes me think a couple of concepts somehow got crosswired. I am assuming that book is some (2 dimensional) array of Seat objects from which you want to pick a random one. In order to do so, you need to specify a random choice for each dimension of the array:

    // this should be declared elsewhere because if it's local to bookSeat it will be lost
    // and reinitialized upon each call to bookSeat
    Seat[][] book = new Seat[12][23];
    
    // and this is how, after previous declaration, the function will be called
    Seat theBookedSeat = bookSeat(book);
    
    // Okay, now we have selected a random seat, mark it as booked, assuming Seat has a 
    // method called book:
    theBookedSeat.book();
    
    
    // and this is the modified function.  Note also that function in Java by convention
    // start with a lowercase letter.
    public static Seat bookSeat(Seat[][] x){
        if (x != null){  
            // using Random as shown by chm052
            Random r = new Random();
            // need to pick a random one in each dimension
            book = x[r.nextInt(x.length)][r.nextInt(x[0].length)];  
        }
        return book;  
    }
    

    You should also integrate a test to check whether the selected seat was already booked and repeat the selection:

            do {
                // need to pick a random one in each dimension
                book = x[r.nextInt(x.length)][r.nextInt(x[0].length)];
            while (book.isBooked()); // assuming a getter for a boolean indicating
                                     // whether the seat is booked or not
    

    But a full-random selection like this has a couple of disadvantages:

    1. the selection being random, you can repeatedly fall on already booked seats, and the chances that happens increase with the number of already booked seats. But even with few booked seats you could be really unlucky and see the loop spin around tens of times before it hits an unbooked seat.
    2. you should absolutely test whether there are still unbooked seats left before entering the loop or it will spin indefinitely.

    Therefore it might be a good idea to implement a smarter selection routine, eg by randomly picking a row and a seat and start searching from there until the first free seat is encountered, but for first steps this one should do just fine.

    I hope this is what you wanted to achieve, if not feel free to comment and allow me to correct and adapt.

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

Sidebar

Related Questions

Currently, I have a static factory method like this: public static Book Create(BookCode code)
I have this method which lets me translate the position of an object, animating
I have this method to display the contact numbers in my inbox: public ArrayList<String>
I have this method which leaks ~ 6KB : + (EInspectorFacilityInfo*) newWithNode: (CXMLNode*) node
I have this method (someone else wrote it!) - (CGPDFDocumentRef)getPdf { NSArray *paths =
I have this method that gets called when i press a button and removes
I have this method that changes an larger image source on click. I need
I have this method for grabbing the file name from a string URI. What
I have this method call I have to use... financial_document.assets.length But financial_document.assets could be
I have this method to transfer files using a FTP Server: private void TransferNeededFiles(IEnumerable<string>

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.