I’m a little bit confused, how to do this. I know I can use Random class to generate random numbers, but I don’t know how to specify and generate 8-byte number?
Thanks,
Vuk
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.
You should note that the
java.util.Randomclass uses a 48-bit seed, so not all 8-byte values (sequences of 64 bits) can be generated using this class. Due to this restriction I suggest you useSecureRandomand thenextBytesmethod in this situation.The usage is quite similar to the
java.util.Randomsolution.Here is the reason why a 48-bit seed is not enough:
Randomclass implements a pseudo random generator which means that it is deterministic.Randomdetermines the future sequence of bits.Randomobject.Based on @Peter Lawreys excellent answer (it deserves more upvotes!): Here is a solution for creating a
java.util.Randomwith 2×48-bit seed. That is, ajava.util.Randominstance capable of generating all possiblelongs.