Re: 64 bit random numbers

From: Michael Giagnocavo [MVP] (mggUNSPAM_at_Atrevido.net)
Date: 10/21/03

  • Next message: Michel Gallant: "Re: 64 bit random numbers"
    Date: Tue, 21 Oct 2003 14:07:34 -0600
    
    

    You can use the System.Security.Cryptography.RNGCryptoServiceProvider to get
    a random number generator. Then you should be able to use GetBytes to fill
    a byte array. From there, you can use System.BitConverter to change those
    bytes into UInt64s.
    -mike
    MVP

    "george r smith" <gsmith@budgetext.com> wrote in message
    news:uDDBRd0lDHA.976@tk2msftngp13.phx.gbl...
    > I am trying to compute a an array of four 64 bit random numbers
    > but I am not having any luck. Following is my code. The problem is in
    > the line aUInt64 = UInt64.Parse(aString);
    >
    > I thought that since a byte is 8 bits that an array of eight Bytes would
    > give me a 64 bit number but since some of the strings are comming
    > back with 22 characters the program blows up because the number
    > is too large.
    >
    > The docs say that UInt64 (same as ulong) cannot exceed
    > 18,446,744,073,709,551,615 so I think that is my problem.
    >
    > Can some one show me how to compute this array of 64 bit
    > random numbers. Am I using the wrong library.
    >
    > thanks much
    > grs
    >
    > static void Main(string[] args)
    > {
    > byte[] random = new Byte[8];
    > System.UInt64 aUInt64 = 0;
    > int aLength;
    >
    > RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
    >
    > for (int count = 0; count < 4; count++)
    > {
    > rng.GetBytes(random);
    > String aString = "";
    >
    > for (int i = 0; i < 8; i++)
    > {
    > aString = aString + random[i].ToString();
    > }
    >
    > aLength = aString.Length; // for debug just want to see
    length
    > aUInt64 = UInt64.Parse(aString);
    > keys.SetValue(aUInt64,count);
    >
    > Console.WriteLine(aString);
    > }
    > Console.ReadLine();
    > }
    > }
    >
    >
    >
    >


  • Next message: Michel Gallant: "Re: 64 bit random numbers"