65.9K
CodeProject is changing. Read more.
Home

How to generate many random various numbers?

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (2 votes)

Sep 13, 2011

CPOL
viewsIcon

14321

Found this little peace of code in book and kept it.using System.Security.Cryptography;public static int RandomNumber(int maxvalue){ RandomGenerator random = RandomGenerator.Create(); byte[] r = new byte[1]; random.GetBytes(r); double val = (double)r[0]/byte.MaxValue; ...

Found this little peace of code in book and kept it.
using System.Security.Cryptography;
public static int RandomNumber(int maxvalue)
{
  RandomGenerator random = RandomGenerator.Create();
  byte[] r = new byte[1];
  random.GetBytes(r);
  double val = (double)r[0]/byte.MaxValue;
  int i = (int)Math.Round(val * maxvalue, 0);
  return i; 
}
Like I said not exactly my peace of code. So to whom, thans..