I'm trying to generate a number based on a seed in C#. The only problem is that the seed is too big to be an int32. Is there a way I can use a long as the seed?
And yes, the seed MUST be a long.
I'm trying to generate a number based on a seed in C#. The only problem is that the seed is too big to be an int32. Is there a way I can use a long as the seed?
And yes, the seed MUST be a long.
For anyone seeing this question today, .NET 6 and upwards provides Random.NextInt64, which has the following overloads:
NextInt64()
NextInt64(Int64)
NextInt64(Int64, Int64)
Random
instance using a 64-bit seed, which is unrelated. –
Mercantile Here's a C# version of Java.Util.Random
that I ported from the Java Specification.
The best thing to do is to write a Java program to generate a load of numbers and check that this C# version generates the same numbers.
public sealed class JavaRng
{
public JavaRng(long seed)
{
_seed = (seed ^ LARGE_PRIME) & ((1L << 48) - 1);
}
public int NextInt(int n)
{
if (n <= 0)
throw new ArgumentOutOfRangeException("n", n, "n must be positive");
if ((n & -n) == n) // i.e., n is a power of 2
return (int)((n * (long)next(31)) >> 31);
int bits, val;
do
{
bits = next(31);
val = bits % n;
} while (bits - val + (n-1) < 0);
return val;
}
private int next(int bits)
{
_seed = (_seed*LARGE_PRIME + SMALL_PRIME) & ((1L << 48) - 1);
return (int) (((uint)_seed) >> (48 - bits));
}
private long _seed;
private const long LARGE_PRIME = 0x5DEECE66DL;
private const long SMALL_PRIME = 0xBL;
}
I'd go for the answer provided here by @Dyppl: Random number in long range, is this the way?
Put this function where it's accessible to the code that needs to generate the random number:
long LongRandom(long min, long max, Random rand)
{
byte[] buf = new byte[8];
rand.NextBytes(buf);
long longRand = BitConverter.ToInt64(buf, 0);
return (Math.Abs(longRand % (max - min)) + min);
}
Then call the function like this:
long r = LongRandom(100000000000000000, 100000000000000050, new Random());
Random
instance using a 64-bit seed, which is unrelated. –
Mercantile © 2022 - 2024 — McMap. All rights reserved.
Random
will not necessarily be the same as the C# version. Therefore, it's very unlikely you're going to get the same output given the same input. – CutshallRandom
class (either in C# or Java), is not actually truly random in the mathematical sense. They take the seed and from that seed they perform various functions to give you what seems like a random number. The problem you have here is that both will give you a "random" number but that number won't necessarily be the same, even if the seed is the same, because they calculate that number differently. – CutshallRandom
function matches up, you should get the same output. In that case, you don't even need theRandom
class and so you can create your own method that takes aLong
. – Cutshalljava.lang.Random
and just convert it to C#; it's probably not too hard. The algorithm is here: docs.oracle.com/javase/6/docs/api/java/util/… – Amadoamador