Generating random bytes java

Programming for beginners

Random class provides nextBytes() method, it takes a byte array as argument and fill the input byte array with random bytes.

package com.sample.Random; import java.util.Random; public class RandomDemo  private static void printArray(byte[] byteArr)  for (byte b : byteArr)  System.out.print(b + ","); > > public static void main(String args[])  Random random = new Random(); byte[] b = new byte[20]; random.nextBytes(b); printArray(b); > > 
-56,108,-21,-73,83,1,-2,-117,-52,-66,-96,-51,89,-79,100,-106,121,68,79,81,

‘java.security.SecureRandom’ class provides nextBytes method, it takes byte array as argument and fill the byte array with random bytes. I would prefer to use Secure Random to generate random numbers than Random. It is because, Random class uses System clock to generate the seed, where as SecureRandom class uses random data from operating system (For ex interval betweem keystorkes, cpul cycles etc.,) to generate the seed. SecureRandom is more secure as compared to Random.

package com.sample.Random; import java.security.SecureRandom; public class RandomDemo  private static void printArray(byte[] byteArr)  for (byte b : byteArr)  System.out.print(b + ","); > > public static void main(String args[])  SecureRandom random = new SecureRandom(); byte[] b = new byte[20]; random.nextBytes(b); printArray(b); > > 
9,62,-83,114,6,70,68,-63,-94,33,-100,-75,-91,-36,-60,-26,27,-116,64,-113,

SecureRandom.getInstanceStrong() is avialble in Java1.8. ‘SecureRandom.getInstanceStrong()’ returns a secure random object that was selected by using the algorithms/providers specified in the securerandom.strongAlgorithms (Ex: Windows-PRNG:SunMSCAPI,SHA1PRNG:SUN)

By using below statements, you can get the algorithms specified in ‘securerandom.strongAlgorithms’ property

Источник

Class Random

An instance of this class is used to generate a stream of pseudorandom numbers; its period is only 2 48 . The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald E. Knuth, The Art of Computer Programming, Volume 2, Third edition: Seminumerical Algorithms , Section 3.2.1.)

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random . Java implementations must use all the algorithms shown here for the class Random , for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.

The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits.

Many applications will find the method Math.random() simpler to use.

Instances of java.util.Random are threadsafe. However, the concurrent use of the same java.util.Random instance across threads may encounter contention and consequent poor performance. Consider instead using ThreadLocalRandom in multithreaded designs.

Instances of java.util.Random are not cryptographically secure. Consider instead using SecureRandom to get a cryptographically secure pseudo-random number generator for use by security-sensitive applications.

Источник

Class Random

An instance of this class is used to generate a stream of pseudorandom numbers; its period is only 2 48 . The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald E. Knuth, The Art of Computer Programming, Volume 2, Third edition: Seminumerical Algorithms , Section 3.2.1.)

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random . Java implementations must use all the algorithms shown here for the class Random , for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.

The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits.

Many applications will find the method Math.random() simpler to use.

Instances of java.util.Random are threadsafe. However, the concurrent use of the same java.util.Random instance across threads may encounter contention and consequent poor performance. Consider instead using ThreadLocalRandom in multithreaded designs.

Instances of java.util.Random are not cryptographically secure. Consider instead using SecureRandom to get a cryptographically secure pseudo-random number generator for use by security-sensitive applications.

Источник

Читайте также:  Python counting items in list
Оцените статью