How in C# to generate a random number with certain length in bits?
It's necessary to implement a cryptographic algorithm.
using System.Security.Cryptography;
...
var bytes = new byte[128];
//EDIT: post-Framework, RNGCryptoServiceProvider is obsolete,
//and RNG.Create() has basically always been the way to do it.
using(var rng = RandomNumberGenerator.Create())
rng.GetBytes(bytes);
As far as implementing a crypto algorithm, for academic purposes I say go for it, but the System.Security.Cryptography library has practically every algorithm you could want, and these have been vetted by far better crypto experts than you or I. One bug in the algorithm, even if it works, could render your system vulnerable. So, I don't recommend rolling your own cryptographic primitives for a real-world product.