I am looking for one efficent way to generate random numbers using java . I want it to be fast and highly secured. Unfortunately SecureRandom class in java and its method nextBytes() generate highly secured random numbers but this method takes me quite time . I am looking if there is any implementation of a method with the same aim as the one above which has complexity O(1) or worst case O(n).
-
2For background, could you describe what the application of these random numbers will be? – Tim Biegeleisen Jun 29 '16 at 14:18
-
look here: http://stackoverflow.com/questions/2523492/choosing-random-numbers-efficiently – Faraz Jun 29 '16 at 14:18
-
1What do you mean by O(n) in this context? Is n the number of digits? – Frank Puffer Jun 29 '16 at 14:19
-
@FrankPuffer yes is the number of bytes the file has – ENIO MARKU Jun 29 '16 at 17:58
-
@ENIOMARKU: But then it's pretty obvious that this can never be faster than O(n). – Frank Puffer Jun 29 '16 at 18:02
2 Answers
Any RNG or PRNG is going to be O(N)
or worse1 to generate N random bits / bytes / whatever. (It is an O(N)
operation to copy N bytes / bits ...)
What I think you are really asking is if there are any RNGs or PRNGs that generated numbers fast and / or seed themselves fast. See this Q & A:
1 - Actually, a typical crypto-quality PRNG is O(N)
. For example, Oracle's SecureRandom
uses SHA1 by default, and SHA1 is an O(M)
algorithm when hashing an M
byte message. This leads to an O(N)
PRNG when generating N
bytes. Of course, the constant of proportionality for SHA-1 is rather large ... but that is not relevant to the big-O complexity class of the algorithm.
You can use Apache commons-math3 to generate random numbers. It has got lots of useful methods for this. See interface "RandomGenerator" and its implementing classes.