-- One recent summer day, eager homebuyers crowded into a small, one-bedroom condominium in San Francisco's South of Market neighborhood, eager for the chance to pay $295,000 for an apartment measuring less than 600 square feet, parking not included.

Despite its small size and less-than-perfect location, the cozy apartment is new, and it's a steal: A similar unit in the same building is offered at $479,000.

But to get into this low-priced home, buyers must leave their fate to a roll of the dice. Or, more precisely, the pulling of their name out of a hat.

Here, in the land of the $726,900 median-priced home, people earning an average income can afford about one square inch of house, so they either remain renters, call on more affluent family members for help to buy or vie with other desperate owner-wannabes to buy units at below-market rates through city programs aimed at first-time buyers.

Of course, there are far more people seeking affordable homes than there are units available.

"There's no question that the demand far outstrips the supply of these programs. I'd be reluctant to even put a number on by how much," said Matt Franklin, director of the Mayor's Office of Housing, which administers the city's various first-time homebuyer programs.

"We have a very severe affordability challenge in this town," he said. "Currently, about 12 percent of households in San Francisco can afford an average-priced home."

That 12 percent figure assumes buyers put 30 percent of income toward annual housing costs, he noted. In some markets these days, homeowners are paying up to 50 percent of income toward housing.

The median-priced home in the San Francisco Bay Area rose to $726,900 in the second quarter, a 12.3 percent rise from $647,300 in the same quarter a year ago, according to the National Association of Realtors.

When it comes to affordability, San Francisco's not alone. Nationwide, a typical first-time homebuyer earned just 70 percent of the income needed to buy an average starter home in the second quarter, down from earning about 77 percent in the first quarter, the National Association of Realtors said in August, assuming a 10 percent down payment.

Nationwide, the median starter home cost $177,200 in the second quarter, and the typical first-time homebuyer could afford a home costing $124,200, according to NAR.

To promote homeownership, cities nationwide have instituted various first-time homebuyer programs, including second loans that don't have to be paid until the unit is sold, below-market-rate units offered through lotteries and other aid programs.

In San Francisco, the system for dividing up below-market-rate units used to work on a first-come, first-served basis, but that led to some desperate measures when homes became available.

"There were some bad experiences with folks camping out for three days to be in line. The old system was almost a survival of the fittest," said Franklin, of the Mayor's Office of Housing. With the lottery, "we've brought more consistency."

Winning the lottery will get you into a home, but it doesn't come without its own limitations. San Francisco's below-market-rate homeowners must live in the home rather than rent it out, and when they decide to sell, the price is dictated by a city formula, aimed at keeping the home affordable for moderate-income people.

The sales price formula is based on what a moderate-income buyer can afford, so prices move upward with changes in the median income for the metro area, released by the U.S. Housing and Urban Development Department each year.

Thus, any sellers of below-market-rate units in recent years would have missed the astronomical price gains enjoyed by other San Francisco homeowners. By the second quarter this year, the median price in the Bay area was up more than 40 percent from the median in 2002, while incomes rose incrementally in that time.

But that doesn't mean you don't make gains, particularly if you stay in the home for a number of years, noted Lori Bamberger, deputy director of San Francisco's Mayor's Office of Housing.

She offered a theoretical example: A family of three could have earned up to about $58,000 in annual income in 1997 -- that was the median income then for this metro area as determined by HUD -- so the city would have sold a two-bedroom, below-market-rate unit for $186,771, because that's what the city determined that household could afford.

Today, that same two-bedroom below-market-rate condo is priced at $325,488, based on what the city determines a family of three earning the area median income now -- $85,500 -- can afford.

Thus, if a family bought that condo in 1997 and sold this year, they would have enjoyed a rise in value of 74 percent over those nine years, or about $139,000. Perhaps not as much as someone in a market-based house, but still something. And the gains ensue no matter which direction home values head in the broader market.

Still, that doesn't mean gains are guaranteed: While median income levels usually go up each year, they didn't between 2004 and 2005 for the San Francisco metro area, Bamberger said.

For other city programs, homeowners pay a portion of any gain to the city when they sell. For instance, under the City Second Loan, if the eligible buyer receives a second loan worth, say, 20 percent of the home's value, then the buyer must pay 20 percent of any gain to the city when they sell the house.

The San Francisco Mayor's Office helps about 250 to 300 homebuyers a year through various programs, Franklin estimated. He wouldn't venture a guess as to how many people apply but are denied aid each year.

In the recent lottery for the South of Market condo, after more than a hundred people milled through the apartment, only 11 filed the necessary applications -- one to prequalify for a loan and one for the city program -- to be entered in the lottery.

On lottery day, which took place at the condo, the only applicant to show up in person came in at No. 7 on the list. The 40-something woman didn't want to give her name for this article, but she said this was her fourth lottery attempt since prequalifying for a loan seven months ago.

In an earlier lottery, she got as close as No. 2 on the list. If the winner of that lottery had failed to get approved for a loan, she would have won the home, but that didn't happen.

Before she turned away to walk home, still a renter, she asked plaintively, "Why am I so unlucky?"