input: Write a python function to generate a password of specified length with certain rules.
  assert len(generate_password(10, {})) == 10
  assert len(generate_password(15, {})) == 15
  assert any(c.isupper() for c in generate_password(8, {}))
  assert any(c.islower() for c in generate_password(8, {}))
  assert any(c.isdigit() for c in generate_password(8, {}))
  assert any(c in "@#$%&" for c in generate_password(8, {}))
output: import random
  import string
  
  def generate_password(length, rules):
      if length < 4:
          raise ValueError("Length must be at least 4 to include all required characters.")
      
      characters = string.ascii_letters + string.digits + "@#$%&"
      
      password = []
      password.append(random.choice(string.ascii_uppercase))  # Ensure at least one uppercase letter
      password.append(random.choice(string.ascii_lowercase))  # Ensure at least one lowercase letter
      password.append(random.choice(string.digits))           # Ensure at least one digit
      password.append(random.choice("@#$%&"))                 # Ensure at least one special character
      
      while len(password) < length:
          password.append(random.choice(characters))
      
      random.shuffle(password)
      
      return ''.join(password)