It is vastly better to buy stock, preferably to buy a diversified collection of stocks; if you invest for the long term and are content to get rich slowly, stocks are a good investment.
A lottery, however, gives you a chance in a million of making money, and 999,999 chances out of a million that you've completely wasted your money.