VB6 Random Number Generator in .NET8
I was charged at work with upgrading an old VB6 program to .NET8.
If that wasn't challenging enough, the program was built in such a way that it required the exact behavior of the VB6 Rnd
function.
Sometimes I try to over-engineer things.
After a few dead-ends trying to replicate the behavior of said function, I came up with this:
///
/// The VB6Random class provides methods for
/// generating random numbers in a way that
/// is compatible with VB6.
///
private class VB6Random
{
///
/// Initializes a new instance of the VB6Random
/// class with the specified seed.
///
public VB6Random(int seed)
{
_ = Microsoft.VisualBasic.VBMath.Rnd(-1);
Microsoft.VisualBasic.VBMath.Randomize(seed);
}
///
/// Returns a random float between
/// 0 (inclusive) and 1 (exclusive).
///
public float Next()
{
return Microsoft.VisualBasic.VBMath.Rnd();
}
}
Comments
Post a Comment