Home  >  Java 8

Java 8 Random and SecureRandom Changes Example

By Arvind Rai, December 23, 2014
In java 8, some new methods has been added in Random and SecureRandom classes. These methods are like ints, longs and doubles. They return IntStream, LongStream and DoubleStream respectively. SecureRandom class extends Random class. Here in this page, I will provide an example of password generator.

java.util.Random

Random class generates pseudorandom numbers. For each call of the instance, a pseudorandom number is returned. Random class is thread safe. Random class has some new methods to support java 8.
ints: returns integer values as IntStream.
longs: returns long values as LongStream
doubles : returns double values as DoubleStream
Find the example to generate a password.
RandomDemo.java
package com.concretepage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
public class RandomDemo {
	private static final List<Integer> VALID_PWD_CHARS = new ArrayList<>();
	static {
	    IntStream.rangeClosed('0', '9').forEach(VALID_PWD_CHARS::add);    // 0-9
	    IntStream.rangeClosed('a', 'z').forEach(VALID_PWD_CHARS::add);    // a-z
	}
	public static void main(String[] args) {
	    int passwordLength = 8;
	    System.out.println("---Generated Password---");
	    for(int i=0;i<5;i++) {
	       new Random().ints(passwordLength, 0, VALID_PWD_CHARS.size())
                            .map(VALID_PWD_CHARS::get).forEach(s -> System.out.print((char) s));
	       System.out.println();
	    }
	}
} 
Output will be
---Generated Password---
6mx3dunz
teuf505p
03nym5w3
zez006fc
y9q0rbs3 

java.security.SecureRandom

SecureRandom is cryptographically strong number generator (RNG). SecureRandom extends Random class and is enriched by new methods added in java 8. Find the example for SecureRandom.
SecureRandomDemo.java
package com.concretepage;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
public class SecureRandomDemo {
	private static final List<Integer> VALID_PWD_CHARS = new ArrayList<>();
	static {
	    IntStream.rangeClosed('0', '9').forEach(VALID_PWD_CHARS::add);    // 0-9
	    IntStream.rangeClosed('A', 'Z').forEach(VALID_PWD_CHARS::add);    // A-Z
	    IntStream.rangeClosed('a', 'z').forEach(VALID_PWD_CHARS::add);    // a-z
	    IntStream.rangeClosed('!', '*').forEach(VALID_PWD_CHARS::add);    // !-*
	}
	public static void main(String[] args) {
	    int passwordLength = 8;
	    System.out.println("---Generated Password---");
	    for(int i=0;i<5;i++) {
	        new SecureRandom().ints(passwordLength, 0, VALID_PWD_CHARS.size())
                            .map(VALID_PWD_CHARS::get).forEach(s -> System.out.print((char) s));
		System.out.println();
	    }
	}
} 
Output will be
---Generated Password---
Qq2R%SsQ
7PjxGxkO
xkMgQq2h
dljs4*w%
55"tSGJ5 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us