It hasn't been done randomly or just for fun, it has a very distinct and purposeful reason behind it.
The current format of the keyboard was devised long back in 1870’s by a gentleman named Christopher Sholes. Though, it definitely was not the first format to come up, it didn't take much time to switch to this one. Starting with lexicographic order i.e. A-B-C-D-E-F, after various trials and errors and taking hundreds of cases, Christopher Sholes gradually reached the Q-W-E-R-T-Y. It was really well received (evident from the fact that we still use it).
When the typewriter was invented, it used a metal bar to hold the character alphabets and the other end of the bar was attached to a linkage carrying a carriage with the coated ink. When a key was struck, it would emboss its character on the paper placed beneath the carriage. However, when an operator learned to type at a great speed, a certain flaw invoked. When two letters were struck in quick succession, the bars of the typewriter would entangle and get jammed.
Christopher Sholes found a way out. He proposed that the letters of frequently used letter pairs should be in different rows. For example, ‘C-H’, ‘S-T’, ’T-H’, ‘W-H’ and more. He also formulated that to speed up the typing process, there has to be a regular alternation between two hands. So observing thousands of words, he placed the letters in way that most words would make use of both hands.
He also observed that almost every word in the dictionary carries a vowel. According to him, the most frequently used vowel was ‘A’ and the most frequently used letter (non-vowel) was ‘S’. So he placed ‘A’ and ‘S’ together and chose to keep less common letters like ‘Q’, ‘W’, ‘Z’, ‘X’, ‘C’ around these. This was complemented by placing fairly common letters like ‘M’, ‘N’, ‘L’, ‘K’, ‘O’, ‘P’ at right extremes to create a perfect alternation between both the hands.
All these factors tested with thousands of trials gave us the format that we still use and perhaps would be using till eternity.