-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
Summary or problem description
We need to add two new methods for GetRandom. We are not changing the original GetRandom method. These two methods use ranges as parameters a min and max value. These methods are still predictable and will have the same outcome from the same seed. In this case the seed is the Block.Nonce or the Transaction.Nonce. The outcome should be the same as long as the iteration of calls are the same. So if you call the method 5 times you'll get the same sequence of numbers in order. As an example iteration 1 returns 5, iteration 2 return 80. Now call it again with same seed you'll get iteration 1 return 5, iteration 2 return 80. So you'll have to make sure that node processes GetRandom the same number of times. Which they will, because currently the code adds an index to the seed.
Methods
int GetRandom(int maxValue);
int GetRandom(int minValue, int maxValue);Do you have any solution you want to propose?
public static int GetRandom(int maxValue)
{
var randomProduct = (ulong)maxValue * GetRandom();
var lowPart = (uint)randomProduct;
if (lowPart < maxValue)
{
var remainder = (0u - maxValue) % maxValue;
while (lowPart < remainder)
{
randomProduct = (ulong)maxValue * GetRandom();
lowPart = (uint)randomProduct;
}
}
return (int)(randomProduct >> 32);
}
public static int GetRandom(int minValue, int maxValue)
{
if (minValue > maxValue)
throw new ArgumentOutOfRangeException(nameof(minValue));
return GetRandom(maxValue - minValue) + minValue;
}Where in the software does this update applies to?
- VM