All good advice you are getting GaryG.
Wanna bet you got this 2004 because the previous owner was having the same problem and dumped it?
When I had NET searched up for intermittent starting problems for your Ranger somehow I had it in my head it was a 1994.
No matter. Still a valid way you need to approce this problem too. On the 1990's Ranger series there were numerous cold start complaints finally traced back to a viberation/heat cycle use breakdown in the underhood fuse relay center for the fuel pump relay ground circuit. Some times the broken wire ends insides the insulation jacket just below the roll crimped on female terminal making connection - sometimes would not. Under hood temperature made the change occure. This would have been a manufacturing overcrimp at that junction work stressing hardening the copper wire strands. Opps.
Another pattern failure on that chassis was found to be a grounding circuit back under the bed developing corrosion in the rust belt areas and then not allowing for a good enough in tank fuel pump power. Wet road conditions with conductive salts - OK. COLD - frozen non-conductive, than not enough circuit flow.
Two other common vehicle harness component problems owner/users/repairfolk had found for that series of Ranger causing intermittent running/dieing/starting.
Your 2004 Ranger will defiantly be a wide/deep/ fast processor/memory capably "OBDII" system. 6 production years into ever tightening OBD ll mandates be an advance generation two or three system at that.
Problem even then is it will only code out what it was programmed to detect and cry uncle about.
The fact it will start and run OK on gasoline does rule out much mechanical like jumped timing chains/belts and such.
The later the vehicle chassis IS, the more cards of possibilities in the deck though man.
I've seen these problems traced back to worn intermittent anti-theft ID components in the key transmitter, receiver, module. Your will have this factory pre-installed even if you do not use it.
Once you've verified the obvious then the game gets just too fun.
TomC on old Ford tractors the cranks-no-start-run areas I've ran into many times have been:
1) internally shorting out distributor all ternimals out the side "crab-caps". Crab cap 2.6L Mitsubishi SOHC V-6's bad for that too.
2) Beauty paint insulating the distributor points ground path distributor points plate to distributor body; distributor body to engine block; engine block to chassis. Fought this problem for decades in charging systems on much repainted refreshed diesel equipment to learn this.
Quickest/easiest diag is to 3/23" drill down through both the pos and neg battery terminals Sheet metal terminal screw into these and run your own FOR-SURE parrel circut bypass wires. No improvement. Good. Jump to something else. Circuit performance/functioning improvement. Find which leg pos or neg is helped. Back up that legs bypass circut connections to pinpoint the problem point. Curse the painter man.
Ha! Seen this same make pretty problem on previous working fine $250K CNC machines. Ugly/expensive problems to fix then!