I once purchase a new KDX200 and had problems with it just randomly shutting down, acted like it ran out of gas. I took it back to the dealer and they could not find any problems. I struggle with the symptom while joy riding and while racing enduro's. Was very frustrating. Just had to sit a while, kick a crap load and it would finally start and off I would go until it quit again some undetermined mileage down the trail. A new plug would also get it going but again would die just down the trail. As you say yours will spark sometimes then sometimes not. Thats exactly what my KDX was appearing to be doing. I removed the spark plug like I did a hundred times before to find no arc/spark. Then I turned the plug up to look down into the underside and found it was arcing, not at the tip but instead all the way up in the plug between the side (metal) and the inner ceramic. For some reason the bike doesn't like to run with it arcing up there. New or old plug it would do the same thing. Maybe it was the oil I was burning or maybe it was a weak magneto. Never found out. But I did learn how to live with it. I learned the symptom to watch for when the plug started to misfire and I would then get into the pipe and really rag it for a few hundred feet till it cleared out and would run fine again. Kinda annoying is some of those really gnarly tight sections having to be in the pipe but it worked. Ran it like that for years.
Don't know if your issue is the same but its something else to look at.
Cheers
btw: my plugs were burning clean. they were not fouled. never had it happen on any other bike I've had, even another KDX I had owned.