How do I quickly generate a random prime number, that is for sure 1024 bit long?
Use a library function, such as OpenSSL. There's no need to write this yourself.
Example: http://ardoino.com/7-maths-openssl-primes-random/