A private key is generated using complex mathematical algorithms when you set up a wallet. The process is mostly automatic and random, creating a unique and secure string of characters. The specific algorithm used depends on the cryptographic system in place. For Bitcoin, for instance, the Elliptic Curve Digital Signature Algorithm (ECDSA) is used.