Accuracy is , when you think about it, the same as predictability: the sights on the gun are indicating where the bullet is going to hit. For a gun to be predictable, it has to be doing the same thing over and over again. It's not surprising then that you find the gunpowder has to be uniform, burn the same way each time. This was the biggest problem in the first couple of hundred years, as , without a real knowledge of chemistry, armorers had to work out by trial and error how to make effective gunpowder, making the same ingredients over and over. It was especially hard to figure out how to make and refine saltpeter. This is why for quite some time , gunpowder in the medieval period was used a lot in large cannon. They were quite expensive to make and fire, but they did not have to be very accurate ( shooting at walls does not require very good accuracy) and their targets were very high value: break a wall or bash in the gate of a city, and it could be taken by an army: fail, and that army might have to sit and starve. Small pole-canons also existed, but, again, more as something to be only pointed.
In the 15th c. gunpowder was largely ( though not completely) worked out, and small arms began to appear that could be aimed, not just pointed. In the late 15th c., rifled arms are recorded as being in the inventory of Emperor Maximilian, almost certainly for hunting. I am not clear about use of such rifles by soldiers for the next hundred years: the military would use more and more muskets, after the Battle of Pavia, but rate of fire was more important than accuracy for the military- or, to be more precise, more important than taking the time to load for accuracy. But rifles were used for hunting, and the first troops recorded using rifles in Germany in 1631 were called Jaegers, or hunters. That coincided with the invention of the flintlock ignition system, which was more practical than matchlocks , less expensive than wheel locks. Those Jaeger troops would continue to be used afterwards- notably sent over during the American Revolution in Hessian units. Like the American riflemen, the Jaegers were found to be very useful in skirmishing. Riflemen would from then on pretty much be a standard part of any army.
Otto Münter: Kurzgeschichte der deutschen Jägertruppe. 1986