If you want to swap engines in a 1999 Ranger, you're going to need either a matching explorer or Ranger with the same engine and PATS configuration as yours has.
1999 Ford Rangers have PATS (passive anti-theft system) which means your donor vehicle will need to have PATS as well. Otherwise, you'll need to employ some techniques to get the engine to start.
I highly recommend getting an entire vehicle for the swap, as you'll need to have several parts to do this. Off the top of my head, you'll need the engine (of course), transmission (I believe) and the computer. If the fuel system is different you may need parts from that, you may need exhaust system parts if those are different, etc etc etc. Since your is not a 4x4, then it's best to look out for another non 4x4 Ranger or explorer, as I believe the transmissions are different.
Of course there's much more information to be heard about this than what I'm capable of giving you, but this will certainly help get you started.
Something to be known, however. There are two 4.0 engines in the Ranger family, the OHV and the SOHC, the latter of which is the successor to the former. The SOHC is a better engine than the OHV. The 1999 year model came with the OHV version of the 4.0.
While the OHV may be marginally easier to install, the SOHC is definitely more worth the trouble. You'll have more horsepower and I believe more torque with the SOHC.
Good luck with your swap, it's not easy. The process is straightforward, but actually doing it requires knowledge to be had from experience. I highly recommend having shop manuals for each vehicle. One for your 1999 Ranger and another specifically for the vehicle which you plan to remove the drivetrain from. These are sometimes refereed to as 'bibles'. I'd imagine you could get 'em from ebay.