How large is the domain of System.Random-class?

  • Thread starter Thread starter Jesper Stocholm
  • Start date Start date
J

Jesper Stocholm

Do any of you guys know how large the domain of System.Random-class is? In
Java there is access to something called SecureRandom(), that selects a
value randomly (and uniformly) in a 2^128-bit domain in comparison with the
normal random-method in Java, that "just" selects from a 2^40 bit domain.

Is there any information available like this in .Net?
 
If you need cryptographically strong bytes, then use
System.Security.RNGCryptoServiceProvider.
-mike
MVP
 
Is there a bug with the System.Random Class? For Example:

Using the System.Random() class in version 1.1 of the Dot Net Framework, I
encounter the following behavior.

When I call the .Next method using a Min & Max value, or just a Max value,
the set of numbers that get returned never contain the max value that I
specified.

Anyone know whats going on ?

VB.Net Sample Code:
Sub Main()
Const iMin As Integer = 1
Const iMax As Integer = 10
Dim oRandom As New System.Random
For iIndex As Integer = 1 To 1000000
Dim iRandom As Integer = oRandom.Next(iMin, iMax)
If iRandom = iMax Then
Console.WriteLine("Got the Max Value:" + iRandom.ToString())
Else
Console.WriteLine("Not the Max Value:" + iRandom.ToString())
End If
Next
End Sub


C# Sample Code
[STAThread]
static void Main(string[] args)
{
const int iMin = 1;
const int iMax = 10;
System.Random oRandom = new System.Random();
for(int iIndex = 1; iIndex < 1000000; iIndex++)
{
int iRandom = oRandom.Next(iMin, iMax);
if(iRandom==iMax)
Console.WriteLine("Got the Max Value:" + iRandom.ToString());
else
{
Console.WriteLine("Nope: " + iRandom.ToString());
}
}
}
 
Gary said:
Is there a bug with the System.Random Class? For Example:

Using the System.Random() class in version 1.1 of the Dot Net Framework, I
encounter the following behavior.

When I call the .Next method using a Min & Max value, or just a Max value,
the set of numbers that get returned never contain the max value that I
specified.

Anyone know whats going on ?

Yup - it's working exactly as documented:

<quote>
Return Value

A 32-bit signed integer greater than or equal to minValue and less than
maxValue; that is, the range of return values includes minValue but not
MaxValue. If minValue equals maxValue, minValue is returned.
</quote>
 
Jon,

Thanks for the reply.

Guess I didn't understand Intellisense when it told me "The Upper bound of
the random number returned" for the parameter information.

Or for that matter the Method information on .Next() "Returns a random
number within the specified range" ...

I guess it should read "Returns a random number within the specified range,
but only if you don't want the max value included in that range".

lol ... Little deceptive I guess ...

Thanks again,
G
 
Gary said:
Thanks for the reply.

Guess I didn't understand Intellisense when it told me "The Upper bound of
the random number returned" for the parameter information.

Or for that matter the Method information on .Next() "Returns a random
number within the specified range" ...

I guess it should read "Returns a random number within the specified range,
but only if you don't want the max value included in that range".

lol ... Little deceptive I guess ...

Not really - a lot of ranges in mathematics are given in terms of an
inclusive lower bound but an exclusive upper bound. (It's also the way
every other random number generator I've used works). I agree it could
be a little bit better worded in the brief description, but I don't
think it's entirely unreasonable to require people to actually read the
full method description once, and then just reminded about it.
 
Back
Top