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"

    Relevant Pages

    • Re: foreach enhancement
      ... > that all variables in the array literal have the same type. ... > If it is not present in the type we have to explicity state an increment ... >> rename it to isin) as you suggested so that it doesn't emit the generator ... With lists you end up with two different sets of values added, ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Assigning generator expressions to ctype arrays
      ... You have argued against using a temporary array. ... But that's exactly what happens in tuple unpacking: the generator on the ... This has the important property that the assignment is atomic: ...
      (comp.lang.python)
    • Re: Simple optimization
      ... number in the array. ... might begin to see patterns in the simulation. ... generator once, make as many numbers as needed, then get rid of it. ... certain areas - the real world doesn't go in for normal distributions as ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: random generator for a given period
      ... > that memory seems cheap without being so. ... Then you can sort on the random numbers with the array of indices passive. ... So what I'm looking for is a random numbers generator of a given period and with the range equal to the period. ... Tighter algorithms are often significantly faster. ...
      (sci.stat.math)
    • Re: random generator for a given period
      ... array such that the same index is never visited more than once. ... So what I'm looking for is a random numbers generator of a given period and with the range equal to the period. ... How does this connect with your discussion of subgroups? ... Well mostly I'm in this for the intellectual challenge, but I'll note that memory seems cheap without being so. ...
      (sci.stat.math)