✅ 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
43 Replies
ero
ero6d ago
Cast it...?
ElectricTortoise
ElectricTortoiseOP6d ago
i meant how do i get out a fully random 64 bit number i can obviously cast it
Pedro Paulo
Pedro Paulo6d ago
There are some overloads for the method, and one of them you can specify the inclusive min value, just specifies it as 0 and the max value as long.MaxValue
ElectricTortoise
ElectricTortoiseOP6d ago
and then cast that to ulong?
ero
ero6d ago
No? You'd need long.MinValue as the min The 64th bit is the sign
ElectricTortoise
ElectricTortoiseOP6d ago
ahhh okok i think i can do that i can cast that to ulong right
ElectricTortoise
ElectricTortoiseOP6d ago
this would work right
No description
canton7
canton76d ago
Yeah, that should work. (I'd use long.MinValue and long.MaxValue)
Pedro Paulo
Pedro Paulo6d ago
I don't think so Int64.MinValue is a negative value you want a positive value, unsigned long
ElectricTortoise
ElectricTortoiseOP6d ago
that's why i have the type cast there
Pedro Paulo
Pedro Paulo6d ago
maybe use Math.Abs to convert it to positive
ElectricTortoise
ElectricTortoiseOP6d ago
...then what is that point
Pedro Paulo
Pedro Paulo6d ago
I think it'll faill, your cast
ElectricTortoise
ElectricTortoiseOP6d ago
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
Kringe6d ago
ero
ero6d ago
Too slow
ElectricTortoise
ElectricTortoiseOP6d ago
there is an override
ero
ero6d ago
*overload
ElectricTortoise
ElectricTortoiseOP6d ago
this will take any int64, inclusive of negative ones
No description
ero
ero6d ago
I'd possibly wrap the cast in unchecked
ElectricTortoise
ElectricTortoiseOP6d ago
ah yea ooo ok it seems to be working without that tho
ElectricTortoise
ElectricTortoiseOP6d ago
see
No description
ElectricTortoise
ElectricTortoiseOP6d ago
ok that's settled
canton7
canton76d 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
ElectricTortoiseOP6d ago
oh so slap unchecked on it gotchu
Pedro Paulo
Pedro Paulo6d ago
ohh, now I see your point, you want to have a random value starting from 0 and ending at ulong.MaxValue, which is greater than long. The solution I said was to have only positive numbers, but not attending the whole range of ulong.
ElectricTortoise
ElectricTortoiseOP6d ago
yes
canton7
canton76d 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
Pedro Paulo
Pedro Paulo6d ago
For this case I'd say you should use RandomNumberGenerator instead of the Random class
canton7
canton76d ago
... Why?
ElectricTortoise
ElectricTortoiseOP6d ago
i need the number that comes out to be fixed i seeded the random
canton7
canton76d ago
(That also doesn't have a method for Int64's, FWIW)
ElectricTortoise
ElectricTortoiseOP6d ago
it's ok the Random.NextInt64() works just gotta tweak it a lil
Pedro Paulo
Pedro Paulo6d ago
nevermind then
canton7
canton76d ago
I don't understand how RandomNumberGenerator makes it any easier to generate a random ulong ><
Pedro Paulo
Pedro Paulo6d ago
I've never said it'd be easier 😛 I misunderstood his problem, I thought he wanted to generate a ulong value totally random, that's why I said to use the RandomNumberGenerator and not because it'd be easier, and that's also why I said "nevermind" because I realized I understood everything incorrectly when he said he had to seed the random numbers.
canton7
canton76d ago
I don't know how you determined that he needed a cryptographically secure random number from that discussion 😛
Anton
Anton6d ago
use the Span<byte> overload, then reinterpret cast to ulong with Unsafe.As @ElectricTortoise
ElectricTortoise
ElectricTortoiseOP6d ago
it's ok i have an easier solution already
Evyr
Evyr6d ago
why not just Unsafe.BitCast<long, ulong> ?
canton7
canton76d ago
Why use Unsafe when you can just use a normal cast?
Evyr
Evyr6d ago
fair
Anton
Anton6d 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?