Non zero Random Number Generato

Generating Non Zero Random Numbers


The RandomNumberGenerator is an abstract class which is used for implementing the
random numbers generation in cryptograhic devices.

using System.Text;
using System.Security.Cryptography; //imp
static void Main(string[] args)
{
byte[] nonZeroValue = new byte[16]; // array is filled up by bytes
// now the abstract class represents specific RNG implementation
RandomNumberGenerator rng = new RNGCryptoServiceProvider();
// now generate random nonzero values
rng.GetNonZeroBytes(nonZeroValue);
Console.WriteLine("Non-zero random value ="+ Encoding.ASCII.GetString(nonZeroValue));
}


note:the vales generated are not cryptographically very strong random values, for generating really strong ones use the derived class RNGCryptoServiceProvider.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: