Changed implementation of random identifier.

This commit is contained in:
Holger Boerchers
2018-08-12 20:11:01 +02:00
parent 9c5a1e7ab8
commit ae35676873
7 changed files with 48 additions and 15 deletions

33
RandomHelper.cs Normal file
View File

@@ -0,0 +1,33 @@
using System;
using System.Security.Cryptography;
namespace SmallInjectorDemo
{
public static class RandomHelper
{
public static int NewRandomInteger(int min, int max)
{
// The random number provider.
using (var rand = new RNGCryptoServiceProvider())
{
uint scale = uint.MaxValue;
while (scale == uint.MaxValue)
{
// Get four random bytes.
var four_bytes = new byte[4];
rand.GetBytes(four_bytes);
// Convert that into an uint.
scale = BitConverter.ToUInt32(four_bytes, 0);
}
// Add min to the scaled difference between max and min.
return (int)(min + (max - min) *
(scale / (double)uint.MaxValue));
}
}
}
}