Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

How to Generate Random Number in Java with Some Variations?

$
0
0

randomnumber crunchify

In Java, there is a method random() in the Math  class, which returns a double value between 0.0 and 1.0. In the class Random there is a method nextInt(int n) , which returns a random value in the range of 0 (inclusive) and n (exclusive).

I’m sure you must have faced below questions in past:

  • How to generate random numbers in Java?
  • Generating random number in a range with Java
  • Where can I find Java Random Numbers Examples?
  • Java.lang.Math.random() Method Example

Here is a simple example which uses Random() function and provides answer to all of your questions. This is what we are doing here:

  • Create method RandomTest1() which is a simple test which prints random number between min and max number (Number Range Example).
  • Create method RandomTest2() which is a simple test which prints random entry from the list.
  • Create Two Threads in main and run Main method.

package com.crunchify.tutorials;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author Crunchify.com
 * 
 */

public class CrunchifyRandomNumber {

	// Simple test which prints random number between min and max number (Number
	// Range Example)
	public void RandomTest1() throws InterruptedException {
		while (true) {
			int min = 5;
			int max = 15;

			float randomNumber = (min + (float) (Math.random() * ((max - min))));
			System.out.println("Test1: " + randomNumber);
			Thread.sleep(500);
		}
	}

	// Simple test which prints random entry from list below
	public void RandomTest2() throws InterruptedException {
		List<String> list = new ArrayList<String>();
		list.add("eBay");
		list.add("Paypal");
		list.add("Google");

		Random randomNumber = new Random();
		String randomEle;
		int listSize = list.size();

		while (true) {
			randomEle = list.get(randomNumber.nextInt(listSize));
			System.out.println("Test2: " + randomEle);
			Thread.sleep(800);
		}
	}

	static public void main(String[] args) {

		// Let's run both Test1 and Test2 methods in parallel..
		Thread thread1 = new Thread() {
			public void run() {
				CrunchifyRandomNumber cr = new CrunchifyRandomNumber();
				try {
					cr.RandomTest1();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};
		Thread thread2 = new Thread() {
			public void run() {
				CrunchifyRandomNumber cr = new CrunchifyRandomNumber();
				try {
					cr.RandomTest2();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};
		thread1.start();
		thread2.start();
	}
}

Eclipse Console Output:

Test2: Google
Test1: 7.8832884
Test1: 12.943945
Test2: eBay
Test1: 5.137196
Test1: 8.76433
Test2: eBay
Test1: 11.420975
Test2: Paypal
Test1: 14.962695
Test1: 5.5223393
Test2: Paypal
Test1: 13.584535
Test2: Paypal
Test1: 11.888685
Test1: 13.628891
Test2: Google
Test1: 12.112564

You may want to take a look at more than 500 tutorials on Java.

Have a suggestion on article? Please chime in and share it as a comment.

The post How to Generate Random Number in Java with Some Variations? appeared first on Crunchify.

Author: App Shah

Crunchify, LLC Logo


Viewing all articles
Browse latest Browse all 1037

Trending Articles