How can I generate random Int64 and UInt64 values using the Random class in C#?
How can I generate random Int64 and UInt64 values using the Random class in
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This should do the trick. (It’s an extension method so that you can call it just as you call the normal
NextorNextDoublemethods on aRandomobject).Just replace
Int64withUInt64everywhere if you want unsigned integers instead and all should work fine.Note: Since no context was provided regarding security or the desired randomness of the generated numbers (in fact the OP specifically mentioned the
Randomclass), my example simply deals with theRandomclass, which is the preferred solution when randomness (often quantified as information entropy) is not an issue. As a matter of interest, see the other answers that mentionRNGCryptoServiceProvider(the RNG provided in theSystem.Securitynamespace), which can be used almost identically.