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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:39:41+00:00 2026-05-22T18:39:41+00:00

I have a method which generates me number of passengers to reservation process. The

  • 0

I have a method which generates me number of passengers to reservation process. The parameters are the minimum and maximum numbers of passengers.

There are only 2 conditions:

  • number_of_adults must be greater than 0
  • number_of_infants must be less than or equal to number_of_adults

This is code:

    def generate_numbers_of_paxes(min_number=1,max_number=9):

    number_of_passengers=random.randint(min_number, max_number)
    print(number_of_passengers)
    number_of_adults=random.randint(min_number, number_of_passengers)
    number_of_youths=random.randint(0, number_of_passengers-number_of_adults)
    number_of_children=random.randint(0, number_of_passengers-number_of_adults-number_of_youths)
    number_of_infants=random.randint(0, number_of_passengers-number_of_adults-number_of_youths-number_of_children)
    number_of_adults+=number_of_passengers-number_of_adults-number_of_youths-number_of_children-number_of_infants
    if(number_of_adults<number_of_infants):
        difference=number_of_infants-number_of_adults
        number_of_infants-=difference
        number_of_adults+=difference

    return [number_of_adults,number_of_youths,number_of_children,number_of_infants]

numbers_of_paxes=generate_numbers_of_paxes()
for (x,y) in enumerate(numbers_of_paxes):
    print(x,y)

This code works good, but as you can see there is some problem in line

number_of_adults+=number_of_passengers-number_of_adults-number_of_youths-number_of_children-number_of_infants

because sum of number for every age group isn’t always equals number_of_passegers, and I add difference to number_of_adult.

  • 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-22T18:39:42+00:00Added an answer on May 22, 2026 at 6:39 pm

    Your number_of_infants calculation should be simply:

    number_of_infants = number_of_passengers - number_of_adults - number_of_youths - number_of_children
    

    Otherwise it is likely to be lower than the required number to meet your passenger requirements (a random number between 0 and number_of_passengers - number_of_adults - number_of_youths - number_of_children).

    However… you are better calculating the number of infants before adults and then calculating the number of adults with number_of_infants as the minimum for the random(), given the constraints you listed. Leave the number of children until last and apply the same logic. Since this looks like homework, I’ll not provide the code for this.

    That is to say:

    1. Generate random number of passengers

    2. Generate random number of infants
      (can’t be more than half of number
      of passengers)

    3. Generate random number of adults
      (must be at least number of infants)

    4. Generate random number of youths
      (can’t be more than passengers less
      infants less adults)

    5. Number of children is number of
      passengers less previous three
      groups.

    Also, why are you doing this?

    for (x,y) in enumerate(numbers_of_paxes):
        print(y)
    

    Either do:

    for (x,y) in enumerate(numbers_of_paxes):
        print(x, y)
    

    Or:

    for y in numbers_of_paxes:
        print(y)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method, which generates a random number, heres the code for it:
In short i have a method which selects a random operation and generates two
I have a simple code which generates random numbers SecureRandom random = new SecureRandom();
I have java method which encrypts a string: public String getCurrency(String hash){ try {
I have static method which plays music. How to return value from this method
I have a method which takes params object[] such as: void Foo(params object[] items)
I have a method which should be executed in an exclusive fashion. Basically, it's
I have an method which save files to the internet, it works but just
I have a method which is given the parameter bool sortAscending. Now I want
I have a method which should be delayed from running for a specified amount

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.