-1

I am trying to generate random number using RNGCryptoServiceProvider, but some how I am getting o(Zero) in return

private int generatePassword()
{

    using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
    {
        string value = string.Empty;
        byte[] randomnumber = new byte[10000];
        rng.GetBytes(randomnumber);
       return randomnumber;

    }

}
akshay_zz
  • 57
  • 1
  • 10
  • it is not really clear to me why you call `ToString` on a byte array. It will return the name of the class: `System.Byte[]`. How do you intend to get the value? please describe the thought behind your code – Mong Zhu Mar 24 '17 at 07:16
  • Your edits aren't helping - you're just producing *uncompilable* code now. Please, instead, take some time, create a [mcve] and [edit] that into your question. Best done in front of a compiler rather than your browser. – Damien_The_Unbeliever Mar 24 '17 at 07:44

1 Answers1

0

Perhaps you should take a look at this link here

dotNetFiddle

static string RandomString(int length)
{
    const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    StringBuilder res = new StringBuilder();

    using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
    {
        byte[] uintBuffer = new byte[sizeof(uint)];

        while (length-- > 0)
        {
            rng.GetBytes(uintBuffer);
            uint num = BitConverter.ToUInt32(uintBuffer, 0);
            res.Append(valid[(int)(num % (uint)valid.Length)]);
        }
    }

    return res.ToString();
}
Community
  • 1
  • 1
Sudarpo Chong
  • 608
  • 4
  • 12