Try something like this:
public class RandomHash
{
private readonly Dictionary<int, int> _mydictionary;
private List<int> _usedNumbers;
private readonly Random _rnd;
private readonly int _size;
public RandomHash(int size)
{
_mydictionary = new Dictionary<int, int>();
_usedNumbers = new List<int>();
_rnd = new Random(100); // magic seed
_size = size;
}
public int HashNumber(int num)
{
if (_mydictionary.ContainsKey(num))
return _mydictionary[num];
int n = _rnd.Next(1, _size);
while (n == num || _mydictionary.ContainsKey(n))
{
n = _rnd.Next(1, _size);
}
_mydictionary.Add(num, n);
return n;
}
}
The magic here is that I'm seeding the Random class with a constant number, 100
. This guarantees that the same numbers will hash to the same "random" number. Use it like this:
static void Main(string[] args)
{
var rh = new RandomHash(5000);
Console.WriteLine(rh.HashNumber(1));
Console.WriteLine(rh.HashNumber(2));
Console.WriteLine(rh.HashNumber(3));
Console.WriteLine(rh.HashNumber(1000));
Console.WriteLine(rh.HashNumber(2));
Console.WriteLine(rh.HashNumber(1));
Console.WriteLine();
}