✅ Random help

How do I get a random ulong out of Random.Next()? Random.NextInt64() seems to actually only give a random 63-bit number
29 Replies
ero
ero2mo ago
Cast it...?
ElectricTortoise
ElectricTortoiseOP2mo ago
i meant how do i get out a fully random 64 bit number i can obviously cast it and then cast that to ulong?
ero
ero2mo ago
No? You'd need long.MinValue as the min The 64th bit is the sign
ElectricTortoise
ElectricTortoiseOP2mo ago
ahhh okok i think i can do that i can cast that to ulong right
ElectricTortoise
ElectricTortoiseOP2mo ago
this would work right
No description
canton7
canton72mo ago
Yeah, that should work. (I'd use long.MinValue and long.MaxValue)
ElectricTortoise
ElectricTortoiseOP2mo ago
that's why i have the type cast there ...then what is that point that would turn -1 into 1 which leaves me with the same 63 bits worth of numbers well then what does Math.Abs do
Kringe
Kringe2mo ago
ero
ero2mo ago
Too slow
ElectricTortoise
ElectricTortoiseOP2mo ago
there is an override
ero
ero2mo ago
*overload
ElectricTortoise
ElectricTortoiseOP2mo ago
this will take any int64, inclusive of negative ones
No description
ero
ero2mo ago
I'd possibly wrap the cast in unchecked
ElectricTortoise
ElectricTortoiseOP2mo ago
ah yea ooo ok it seems to be working without that tho
ElectricTortoise
ElectricTortoiseOP2mo ago
see
No description
ElectricTortoise
ElectricTortoiseOP2mo ago
ok that's settled
canton7
canton72mo ago
It will throw if you're in a checked context, either because someone wrapped your method in checked, or if the assembly was compiled with checked arithmetic
ElectricTortoise
ElectricTortoiseOP2mo ago
oh so slap unchecked on it gotchu yes
canton7
canton72mo ago
Yeah, it's one of those things that normally doesn't matter, but is good practice just for the odd case where it does matter for some reason ... Why?
ElectricTortoise
ElectricTortoiseOP2mo ago
i need the number that comes out to be fixed i seeded the random
canton7
canton72mo ago
(That also doesn't have a method for Int64's, FWIW)
ElectricTortoise
ElectricTortoiseOP2mo ago
it's ok the Random.NextInt64() works just gotta tweak it a lil
canton7
canton72mo ago
I don't understand how RandomNumberGenerator makes it any easier to generate a random ulong >< I don't know how you determined that he needed a cryptographically secure random number from that discussion 😛
Anton
Anton2mo ago
use the Span<byte> overload, then reinterpret cast to ulong with Unsafe.As @ElectricTortoise
ElectricTortoise
ElectricTortoiseOP2mo ago
it's ok i have an easier solution already
Evyr
Evyr2mo ago
why not just Unsafe.BitCast<long, ulong> ?
canton7
canton72mo ago
Why use Unsafe when you can just use a normal cast?
Evyr
Evyr2mo ago
fair
Anton
Anton2mo ago
it's been established that it's positive, it's missing a bit of information and the span overload is more flexible, if you know what you're doing like if you're going to need 100 numbers, you can stackalloc a buffer for all of them and generate them all at once

Did you find this page helpful?