Pseudorandom integral number generator - please help!!

A

almurph

Hi,

I'm trying to generate a pseudorandom *integral* number between the
range [min, max) of cryptographic quality.
At this moment in time as far as I've gotten is the ability to
generate a pseudorandom number between [0, 1) which is shown below.
Can you help me please achieve what i am trying to do? I do not wish
to use Random. I need better "quality" numbers...

Thanks in advance for any comments/suggestions/help code-samples that
you may be able to offer.

Al.

**** CODE AS FOLLOWS ****


//Pseudorandom number (of crypto strength) in range [0.0,1.0)
private double GetRandNumCrypto()
{
byte[] salt = new byte[8];
RNGCryptoServiceProvider rng = new
RNGCryptoServiceProvider();
rng.GetBytes(salt);
return (double)BitConverter.ToUInt64(salt, 0) /
UInt64.MaxValue ;

}
 
A

Arne Vajhøj

Hi,

I'm trying to generate a pseudorandom *integral* number between the
range [min, max) of cryptographic quality.
At this moment in time as far as I've gotten is the ability to
generate a pseudorandom number between [0, 1) which is shown below.
Can you help me please achieve what i am trying to do? I do not wish
to use Random. I need better "quality" numbers...

Thanks in advance for any comments/suggestions/help code-samples that
you may be able to offer.

Al.

**** CODE AS FOLLOWS ****


//Pseudorandom number (of crypto strength) in range [0.0,1.0)
private double GetRandNumCrypto()
{
byte[] salt = new byte[8];
RNGCryptoServiceProvider rng = new
RNGCryptoServiceProvider();
rng.GetBytes(salt);
return (double)BitConverter.ToUInt64(salt, 0) /
UInt64.MaxValue ;

}

Try:

return BitConverter.ToUInt64(salt, 0) % (max - min) + min;

If (max - min) is huge compared to UInt64.MaxValue or you want it to be
perfect, then you need to discard values higher than multipla
of (max - min).

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top