Java Random – generating random objects

Today we are going to show you how to generate random objects in Java. As a lot of things in Java this task is easy, all you need to know is how to use the Random class.

ITCuties - Java Random

ITCuties – Java Random

You can create a Random object using a no argument constructor.

Random random = new Random();

This code creates a Random object.
To generate a random values you need to call one of the next* method. If you need to generate some positive random values just use the Math.abs method on the next* Random class method output. See the below snippet.

// Create the Random object
Random random = new Random();
		
// Get a random BOOLEAN value
System.out.println("Random boolean: " + random.nextBoolean());
		
// Get a random INT value
System.out.println("Random int: " + random.nextInt());
System.out.println("Random positive int: " + Math.abs(random.nextInt()));
// Get a random BOOLEAN value from the set of [0,1,2,3,4]
System.out.println("Random int in range [0,4]: " + random.nextInt(5));
		
// Get a random LONG value
System.out.println("Random long: " + random.nextLong());
System.out.println("Random positive long: " + Math.abs(random.nextLong()));
		
// Get random floating point values FLOAT and DOUBLE
System.out.println("Random float:" + random.nextFloat());
System.out.println("Random positive float:" + Math.abs(random.nextFloat()));
System.out.println("Random double:" + random.nextDouble());
System.out.println("Random positive double:" + Math.abs(random.nextDouble()));
		
// Get a random BYTE array value
byte[] randomBytes = new byte[16];
// Write random bytes to an array
random.nextBytes(randomBytes);	
System.out.print("Random bytes: " );
for (byte b: randomBytes) {
	System.out.print(b + "| ");
}

This code will give an output that is similar to this:

Random boolean: false
Random int: 2045949772
Random positive int: 1637005597
Random int in range [0,4]: 0
Random long: 7485659333153119156
Random positive long: 1924800545120770834
Random float:0.8219591
Random positive float:0.88428867
Random double:0.5182285088769584
Random positive double:0.8397869142998297
Random bytes: 122| 7| 14| 112| 49| -72| -104| -110| 43| -12| -116| 69| 3| 12| -98| 63|

Using seed value

If you have a seed value you can use it to initialize your object with it, to start with a given random object state. It is done this way.

Random random = new Random(1000);

To see how does seed work see the below code where we create a Random object 10 times and call nextDouble() method each time.

// Use the same seed value for each of the 10 Random objects
long seedValue = 1;
for (int i=0; i < 10; i++) {
	Random seedRandom = new Random(seedValue);
	System.out.println(seedRandom + ": " + seedRandom.nextDouble());
}

The result of running this code results in…

java.util.Random@de6ced: 0.7308781907032909
java.util.Random@c17164: 0.7308781907032909
java.util.Random@1fb8ee3: 0.7308781907032909
java.util.Random@61de33: 0.7308781907032909
java.util.Random@14318bb: 0.7308781907032909
java.util.Random@ca0b6: 0.7308781907032909
java.util.Random@10b30a7: 0.7308781907032909
java.util.Random@1a758cb: 0.7308781907032909
java.util.Random@1b67f74: 0.7308781907032909
java.util.Random@69b332: 0.7308781907032909

… the same random value for different object :)

Download this sample code here.

This code is available on our GitHub repository as well.

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>