SecureRandom nextBytes() method in Java with Examples
The nextBytes() method of java.security.SecureRandom class is used to generate a user-specified number of random bytes.
If a call to setSeed had not occurred previously, the first call to this method forces this SecureRandom object to seed itself. This self-seeding will not occur if setSeed was previously called.
public void nextBytes(byte bytes)
Parameters: This method takes the array to be filled in with random bytes as parameter .
- The programs will not run on online IDE.
- Every time Secure Random class will generate random output.
Below are the examples to illustrate the nextBytes() method:
Byte array before operation : [84, 97, 106, 109, 97, 104, 97, 108] Byte array after operation : [-79, -110, -18, -31, -54, -36, 63, -61]
Note: The following program throws the following Exception on GeeksForGeeks IDE but it will run efficiently on any Command Prompt(JDK)
Exception thrown : java.security.ProviderException: init failed
Byte array before operation : [84, 97, 106, 109, 97, 104, 97, 108] Byte array after operation : [-14, 77, 123, 121, 116, 50, -89, -86]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.