The straightforward answer is that codes for Battlefield 3 come with purchases. Retail copies, limited editions, and promotional bundles have all included them, and Origin or Steam keys are delivered at checkout when you buy digitally.
Hunting for free codes online rarely ends well. The listings are mostly expired, and a meaningful share are outright scams designed to harvest logins or survey data. Anything advertised as a key generator is not real.
Buying the game is usually cheaper than the time spent chasing a working code, and it comes with the assurance that the key will actually activate. Watch for sales, since Battlefield titles are discounted heavily and regularly.