Generating Random 64 Bit Integers In C# & .NET
[C#, .NET]
When generating random integers
in .NET, the goto is the Next() method of the Random class.
var randomNumber = Random.Shared.Next();
Console.WriteLine(randomNumber);
This will print something like this:
34234234
This, however, just gives you a 32 bit integer
int, bounded from int.MinValue (-2,147,483,648) and int.MaxValue (2,147,483,648
)
What if you wanted a larger number, a 64-bit integer
- Int64 (or long
).
This is bounded by long.MinValue (-9,223,372,036,854,775,808
) and long.MaxValue (-9,223,372,036,854,775,808
)
Integral types and their sizes are covered in the post The Other Integer Types
The Random
class supports this via the NextInt64() method.
var randomNumber = Random.Shared.NextInt64();
Console.WriteLine(randomNumber);
This will print something like this:
8588670651705391622
TLDR
The Random
class supports generation of 64 bit integers using the NextInt64()
method.
Happy hacking!