And as for the example with RNGCryptoServiceProvider, it can't even work and
isn't reentrant.
byte[] randbyte = new byte[0]; ????
public int getRandomNumber(int min, int max)
{
rng.GetNonZeroBytes(randbyte);
// will always throw IndexOutOfRangeException
Random rand = new Random(Convert.ToInt32(randbyte[0]));
return rand.Next(min, max);
}
Even if there was one byte allocated, the generator would always be able to
generate only 255 random values (though in random order).
It could possibly work like this, though:
public class RandomNumberGenerator
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
public int getRandomNumber(int min, int max)
{
byte[] rand = new byte[4];
rng.GetBytes(rand);
int num = BitConverter.ToInt32(rand, 0);
return min + num % (max - min);
}
}
But I'm sure that the basic Random() class should be more than sufficent for
any non-cryptographical use.
HTH,
Stefan
"Glenn Wilson" <ir****@hotmail.com> wrote in message
news:em**************@TK2MSFTNGP15.phx.gbl...
I use the following... Gives Better results...
using System;
using System.Security.Cryptography; // Needed for a descent random number
generator.
namespace Utilities
{
// This random generator uses the crypto service provider because the
normal random functions in .NET
// have repeating number strings and other such problems.
public class RandomNumberGenerator
{
byte[] randbyte = new byte[0];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
public int getRandomNumber(int min, int max)
{
rng.GetNonZeroBytes(randbyte);
Random rand = new Random(Convert.ToInt32(randbyte[0]));
return rand.Next(min, max);
}
}
}
"Starbuck" <St******@tisconi.co.uk> wrote in message
news:OS****************@TK2MSFTNGP14.phx.gbl... Hi Peter
That looks like it might suit our needs, Thanks
"Peter Jausovec" <pe***@jausovec.net> wrote in message
news:ek****************@TK2MSFTNGP12.phx.gbl... Hi,
I am using this code for random numbers:
Random rnd = new Random (DateTime.Now.Millisecond);
int randomNumber = rnd.Next ();
Regards,
Peter Jausovec
(http://blog.jausovec.net)
"Starbuck" <St******@tisconi.co.uk> wrote in message
news:OR****************@tk2msftngp13.phx.gbl...
Hi
In VB6 we used the following to create a unique random number -
Function longSerial() As Long
longSerial = Val((Format$(Int(Rnd * 424) - 212)) + Format$((Timer *
100), "0000000"))
longSerial = longSerial Xor Int(2147483647 * Rnd)
End Function
Seem to be unable to do similar in c#, can anyone assist
Thanks