random number question

S

Starbuck

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
 
G

Glenn Wilson

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);

}

}

}
 
S

Stefan Simek

I still don't see a point why would you always create a new instance of the
Random class. Once you create it, it's supposed to return different values
for the next 2^32 calls of the Next method.
If you do new Random(DateTime.Now.Milliseconds).Next(), your "random" number
changes only with the frequency of DateTime.Now changes (16 ms for me), and
it repeats every second!!!

a simple:

static Random rnd = new Random();

and using rnd.Next() anytime you use it should be all that you need...

HTH,
Stefan

Glenn Wilson said:
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 said:
Hi Peter

That looks like it might suit our needs, Thanks
 
S

Stefan Simek

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 said:
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 said:
Hi Peter

That looks like it might suit our needs, Thanks
 
S

Starbuck

Thanks Glenn

Glenn Wilson said:
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 said:
Hi Peter

That looks like it might suit our needs, Thanks
 

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