Many people dream of winning the lottery, and these days it’s easier than ever to buy a ticket. But it’s not as simple as choosing your numbers and hoping for the best. There’s a lot to consider, including the odds of actually winning a prize, and how money is distributed among winners.

Most lottery games involve picking your own set of numbers and waiting to see if any of them match those that are randomly selected. The more of your numbers that match, the larger the prize you win. The prizes themselves vary widely, from cash to goods. Some states have even offered cars or houses for winners. But the entire lottery system isn’t free to operate: Someone has to design the scratch-off games, record live lottery drawing events and keep the websites up to date. These workers and administrative costs add up, so a portion of every winning ticket goes toward overhead.

In addition to the overhead, state-run lotteries also profit from a large portion of each sale. Most retailers sell tickets, and the money they receive for each purchase goes to the lottery commission or other organization that runs the lottery. Some retailers may even make additional money by selling items such as lottery merchandise, which is often marketed to those who already play the game.

The casting of lots for decisions or fate has a long history in human culture, and some historians believe the first recorded public lottery was organized by Roman Emperor Augustus in order to fund repairs in the city of Rome. However, the lottery as we know it today likely originated in the Low Countries in the 15th century. Town records in Ghent, Utrecht and Bruges indicate that the lottery was used to raise funds for town walls and to help the poor.

While the majority of lottery players are middle-class, data suggest that lower-income communities participate in lotteries at a much higher rate than high-income groups. Men tend to play more than women, and blacks and Hispanics play significantly more than whites. In addition, those who are young or old play less frequently than those in the middle age range.

While there are plenty of stories of lottery winners who end up blowing their windfalls or being slammed with lawsuits, there are also those who make wise choices and put their money to good use. One example is former New York lottery winner Steve Mandel, who chose to spend his winnings on a quiet life in Vanuatu, a South Pacific island country known for its volcanoes and waterfalls. NerdWallet has rounded up some tips from financial experts on how to handle a big lottery payout.