The term "Yakuza" literally derives from beginnings of the traditional words "Yattsu (Eight)", "Ku (Nine)" and "Za (Three)" based on the worst hand attainable in the Japanese card game Oicho-Kabu, where the 3 numbers are added to 20 and the last digit is taken as the score, zero, similar to the western game baccarat. Although there is a misconception that the name refers to bad luck, the term "Ya Ku Za" is a literal interpretation from the game which means "good for nothing" or "a bad hand" and is used with reference to their precedence and contribution within Japanese society
Despite their notoriety in modern Japan, the precise origin of the Yakuza is still somewhat the subject of debate. The first historical interpretation of their derivation is from the hatamoto-yakko or Kabuki-mono of the 17th century Genroku Era, who were derivative classes of the low-rank hatamoto, which resembled a quarter of the shogun
Other theories, suggested by the Yakuza members themselves claim their origins are from the machi-yokko, who policed villages by protecting them from the hatamoto-yakko that tried to steal from them, despite them being outmatched by the Hatamoto-yakko in training and strength. Despite their shortcomings, the machi-yakko were regarded as folk heroes similar to those in the stories of Robin hood, with some groups being made the feature of plays and dramas. The derivation from the hatamoto-yakko or Kabuki-mono known for their adoption of strange hair styles and outrageous dress manner refers to a relevant era of the Genroku Period in which kabuki plays, and onnagata were prominent.
Despite the different groups, the majority of the events which led to their inception occurred during the Edo period. As peacetime brought about by the destruction of the Totoyomi Clan ensured the Tokugawa shogunate's role of maintaining peace, shogun retainers were no longer required in their role as soldiers and moved from their own catchment areas to live in feudal castles where their income was determined by their daimyō.
Due to the isolation of Japan and restriction of foreign trade, Japan's agricultural production and domestic trade greatly improved which resulted in the increase of power in the merchant class and the financial dependency of the samurai upon them -- samurai retainers were paid with rice by their daimyō, and then sold in markets as a means of generating their salary.
As natural disasters, famine and tax increases led to the destabilization of the social hierarchy and the decline of morals due to public dissatisfaction with the government, factions of wayward, leaderless samurai known as ronin began to focus their attention from community service towards generating money through theft and violence towards smaller mercantile villages with disparate policing and little feudal control as they presented less-dangerous means of achieving iniquitous money. However, Yakuza that claim origin from the machi-yakko refute their origins from the hatamoto-yakko due to its association with thievery, which is supposedly unpracticed amongst modern Yakuza.
In larger towns, several of these groups often existed simultaneously, and they often fought for territory, money and influence much like modern gangs, disregarding any civilians caught in the crossfire. Again, this is the origin of a popular theme of Japanese film and television, made famous in the West by an Akira Kurosawa film called Yojimbo in which a wandering ronin sets two such gangs against each other and eventually destroys them. Yakuza derived some practices from both machi-yakko and kabukimono. Their protection rackets can be seen as originating from machi-yakko, but their more colorful fashion and language are derived from the kabukimono tradition.