Was nitroglycerin invented as an explosive?
Probably ... but he doesn't actually say

$Nitroglycerin^1$ was discovered by Dr. Ascanio Sobrero in 1847. Both Sobrero and Alfred Nobel were students of Prof. Théophile-Jules_Pelouze.

It is rather unfair to describe chemistry of this era as "random". 1847, of course, was mid-1800s rather than early 1800s, and it was some two decades after Wöhler's famous demonstration that organic chemicals could be synthesized from inorganic ones. Organic chemistry of this era was more in its adolescence than its infancy and was being pursued in a systematic scientific manner with publications in peer reviewed journals.

Some of these journals have now been digitised and are searchable, so we have some idea of Pelouze's work, and his students. Much of it involved preparation and study of nitro-esters of simple carbohydrates. They don't seem to say why, but in the context of the era, it is very likely they were looking for practical explosives. Braconnot had discovered nitrostarch in 1832; Pelouze apparently first prepared nitrocellulose in 1838, but didn't recognise it as a new substance. Then in 1846 -- just one year before Sobrero nitrated glycerin -- no fewer than 3 chemists prepared and described nitrocellulose in the same year, with Otto being first to publish.

For a long time, it appeared that nitrocellulose was not a practical material as it is very difficult to purify, and extremely unstable when impure. So it is not surprising that a year later, we find one of the early nitrocellulose investigators now conducting a program of testing nitrates of a range of simple organic compounds, especially carbohydrates. (Cellulose itself is a carbohydrate.) They don't say exactly why in anything I have read. It is possible that they are using nitrations to investigate fundamental chemistry; for example, such a process does help to identify the structure of simple sugars. However, they test them all for explosive properties and refer to them all as "fulminant" (i.e. explosive.)

Sobrero's paper on his discovery of nitroglycerin has been digitised. (Note: article is in French.) He mentions earlier work with sugars and notes that glycerin is not a carbohydrate, but doesn't seem to say what inspired him to try to nitrate it except that it is "analogous" with other materials that his group had nitrated. Incidentally, in this paper he describes purification methods at some length, and also discusses the toxicity:


  ... for it is sufficient to take a very small quantity (which can be taken
  by slightly wetting the tip of the little finger) on the tongue to
  feel a powerful migraine for many hours. This action on the human body has
  been observed by many people in my laboratory ...


However, he doesn't mention its extreme shock sensitivity. Later, Sobrero came to the conclusion that nitroglycerin was too dangerous to use, and it was, of course, his colleague Alfred Nobel who did the considerably longer and more complicated studies to find a way to render it practicable. (The same situation occurred with nitrocellulose; discovered by Schönbein, Böttger and Otto, but it was Abel who did the real spade work to make a practical industry for it.)




Note that whilst many of the substances in this answer were historically called nitroX where X is the original moiety, none of them are nitro compounds in the modern sense. Rather, all explosives discussed in this answer are nitrate esters. So nitroglycerin, for example, should really be called glyceryl trinitrate.