This is written in swift, it's open source and you can check out the source code to get an idea of what's happening:
https://github.com/BlockchainCommons/GordianSeedTool-iOS
But as I'm coming to understand it myself, I'll attempt to summarize. You need to have a seed (a random number as a starting point) of sufficient entropy, or randomness, that's 2^256 bits long or 10^77, so pretty hard to repeat. Not every random number generator is actually random, for example some kinds of random are repeatable and are great for applications where you need to replicate results. But in this case you want to make it difficult to replicate. Every OS would have it's own set of random number generator syscalls that may or may not be random enough to be cryptographically secure, so you'd have to check the docs on whatever you use to ensure it's secure enough.
https://en.wikipedia.org/wiki/Cryptographically-secure_pseudorandom_number_generator
Generating mnemonic phrases is a little trickier. BIP-39 is the standard for implementing mnemonics in Bitcoin. This methods inserts bits of randomness, and then pulls some of the numbers out and selects the number pulled from an index of a vocabulary wordlist, hence generating the mnemonic phrase. It seems as if this is not quite as secure as a completely random key generated from sufficient entropy, as the possibilities is reduced from 2^256 to 2^132. Still pretty difficult to repeat though. See the following to understand how difficult this is to crack:
https://github.com/BlockchainCommons/SmartCustodyBook/blob/master/manuscript/00-randomness.md
So to generate a private key that can be restored via mnemonic phrase, it must be BIP-39 compatible.
https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
Rather than trying to recreate all this functionality from scratch, which I wouldn't be able to explain succinctly, you could take advantage of libwally, or utilize the BC libraries or peruse them for your own learning/replication.
https://github.com/Sjors/libwally-swift
https://github.com/BlockchainCommons/seedtool-cli
https://github.com/BlockchainCommons/Learning-Bitcoin-from-the-Command-Line/blob/266d7e13a3ba1ce40d29c83a5d63a8d805c06621/src/16_2_genmnemonic.c
https://github.com/BlockchainCommons/Learning-Bitcoin-from-the-Command-Line/blob/45ccb82661b2f4086b9b3bcb062f0c738023aa4b/18_6_Accessing_Bitcoind_with_Swift.md