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.

How to Generate Random Number in Java with Some Variations?

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 crunchify.com.tutorials;

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

/**
 * @author Crunchify.com
 * How to Generate Random Number in Java with Some Variations?
 */

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 = 50;
            int max = 100;

            // random() returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
                        // Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.
            float randomNumber = (min + (float) (Math.random() * ((max - min))));
            System.out.println("Crunchify Thread 1 random result: " + randomNumber);

            // sleep() causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds,
            // subject to the precision and accuracy of system timers and schedulers.
            // The thread does not lose ownership of any monitors.
            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("Facebook");
        list.add("Twitter");
        list.add("Google");

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

        while (true) {
            randomEle = list.get(randomNumber.nextInt(listSize));
            System.out.println("Crunchify Thread 2 random result: " + randomEle);
            Thread.sleep(500);
        }
    }

    static public void main(String[] args) {

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

        // start() Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
                //The result is that two threads are running concurrently: the current thread
                // (which returns from the call to the start method) and the other thread (which executes its run method).
        crunchifyThread1.start();
        crunchifyThread2.start();
    }

}

Eclipse Console Output:

/Library/Java/JavaVirtualMachines/jdk-15.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=55975:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/app/crunchify/github/CrunchifyTutorials/target/classes:/Users/app/crunchify/github/CrunchifyTutorials/WebContent/WEB-INF/lib/zxing-2.1.jar:/Users/app/crunchify/github/CrunchifyTutorials/WebContent/WEB-INF/lib/commons-logging-1.1.2.jar:/Users/app/crunchify/github/CrunchifyTutorials/WebContent/WEB-INF/lib/commons-collections-3.2.1.jar:/Users/app/crunchify/github/CrunchifyTutorials/WebContent/WEB-INF/lib/javax.mail.jar:/Users/app/crunchify/github/CrunchifyTutorials/WebContent/WEB-INF/lib/commons-io-2.4.jar:/Users/app/crunchify/github/CrunchifyTutorials/WebContent/WEB-INF/lib/commons-lang-2.6.jar:/Users/app/crunchify/github/CrunchifyTutorials/WebContent/WEB-INF/lib/commons-configuration-1.9.jar:/Users/app/crunchify/github/CrunchifyTutorials/WebContent/WEB-INF/lib/log4j-1.2.17.jar:/Users/app/crunchify/github/CrunchifyTutorials/WebContent/WEB-INF/lib/commons-beanutils-1.8.3.jar:/Users/app/.m2/repository/org/glassfish/javax.json/1.0.4/javax.json-1.0.4.jar:/Users/app/.m2/repository/com/github/wnameless/json-flattener/0.2.2/json-flattener-0.2.2.jar:/Users/app/.m2/repository/com/eclipsesource/minimal-json/minimal-json/0.9.4/minimal-json-0.9.4.jar:/Users/app/.m2/repository/org/apache/commons/commons-lang3/3.10/commons-lang3-3.10.jar:/Users/app/.m2/repository/com/google/code/gson/gson/2.8.0/gson-2.8.0.jar:/Users/app/.m2/repository/net/jodah/expiringmap/0.5.7/expiringmap-0.5.7.jar:/Users/app/.m2/repository/org/apache/httpcomponents/httpclient/4.3.6/httpclient-4.3.6.jar:/Users/app/.m2/repository/org/apache/httpcomponents/httpcore/4.3.3/httpcore-4.3.3.jar:/Users/app/.m2/repository/commons-codec/commons-codec/1.6/commons-codec-1.6.jar:/Users/app/.m2/repository/org/json/json/20151123/json-20151123.jar:/Users/app/.m2/repository/net/spy/spymemcached/2.12.3/spymemcached-2.12.3.jar:/Users/app/.m2/repository/com/whalin/Memcached-Java-Client/3.0.2/Memcached-Java-Client-3.0.2.jar:/Users/app/.m2/repository/commons-pool/commons-pool/1.5.6/commons-pool-1.5.6.jar:/Users/app/.m2/repository/org/slf4j/slf4j-api/1.6.4/slf4j-api-1.6.4.jar:/Users/app/.m2/repository/com/googlecode/xmemcached/xmemcached/2.4.5/xmemcached-2.4.5.jar:/Users/app/.m2/repository/com/paypal/sdk/rest-api-sdk/1.14.0/rest-api-sdk-1.14.0.jar:/Users/app/.m2/repository/commons-dbcp/commons-dbcp/20030825.184428/commons-dbcp-20030825.184428.jar:/Users/app/.m2/repository/javax/ws/rs/javax.ws.rs-api/2.0/javax.ws.rs-api-2.0.jar:/Users/app/.m2/repository/org/hamcrest/hamcrest-all/1.3/hamcrest-all-1.3.jar:/Users/app/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar:/Users/app/.m2/repository/com/google/guava/guava/19.0/guava-19.0.jar:/Users/app/.m2/repository/com/googlecode/json-simple/json-simple/1.1/json-simple-1.1.jar:/Users/app/.m2/repository/commons-net/commons-net/2.0/commons-net-2.0.jar:/Users/app/.m2/repository/asm/asm/3.3.1/asm-3.3.1.jar:/Users/app/.m2/repository/axis/axis/1.4/axis-1.4.jar:/Users/app/.m2/repository/org/apache/axis/axis-jaxrpc/1.4/axis-jaxrpc-1.4.jar:/Users/app/.m2/repository/axis/axis-wsdl4j/1.5.1/axis-wsdl4j-1.5.1.jar:/Users/app/.m2/repository/commons-beanutils/commons-beanutils/1.8.3/commons-beanutils-1.8.3.jar:/Users/app/.m2/repository/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar:/Users/app/.m2/repository/commons-configuration/commons-configuration/1.10/commons-configuration-1.10.jar:/Users/app/.m2/repository/commons-io/commons-io/2.4/commons-io-2.4.jar:/Users/app/.m2/repository/commons-discovery/commons-discovery/0.5/commons-discovery-0.5.jar:/Users/app/.m2/repository/commons-lang/commons-lang/2.6/commons-lang-2.6.jar:/Users/app/.m2/repository/commons-logging/commons-logging/1.1.3/commons-logging-1.1.3.jar:/Users/app/.m2/repository/commons-logging/commons-logging-api/1.1/commons-logging-api-1.1.jar:/Users/app/.m2/repository/javax/mail/mail/1.4.7/mail-1.4.7.jar:/Users/app/.m2/repository/javax/activation/activation/1.1/activation-1.1.jar:/Users/app/.m2/repository/javax/xml/jaxrpc-api/1.1/jaxrpc-api-1.1.jar:/Users/app/.m2/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar:/Users/app/.m2/repository/org/apache/axis/axis-saaj/1.4/axis-saaj-1.4.jar:/Users/app/.m2/repository/wsdl4j/wsdl4j/1.6.3/wsdl4j-1.6.3.jar:/Users/app/.m2/repository/com/google/zxing/core/3.2.1/core-3.2.1.jar:/Users/app/.m2/repository/org/apache/commons/commons-compress/1.9/commons-compress-1.9.jar:/Users/app/.m2/repository/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar:/Users/app/.m2/repository/junit/junit/4.12/junit-4.12.jar:/Users/app/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/Users/app/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/Users/app/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/Users/app/.m2/repository/org/springframework/spring-context/5.1.3.RELEASE/spring-context-5.1.3.RELEASE.jar:/Users/app/.m2/repository/org/springframework/spring-aop/5.1.3.RELEASE/spring-aop-5.1.3.RELEASE.jar:/Users/app/.m2/repository/org/springframework/spring-beans/5.1.3.RELEASE/spring-beans-5.1.3.RELEASE.jar:/Users/app/.m2/repository/org/springframework/spring-core/5.1.3.RELEASE/spring-core-5.1.3.RELEASE.jar:/Users/app/.m2/repository/org/springframework/spring-jcl/5.1.3.RELEASE/spring-jcl-5.1.3.RELEASE.jar:/Users/app/.m2/repository/org/springframework/spring-expression/5.1.3.RELEASE/spring-expression-5.1.3.RELEASE.jar:/Users/app/.m2/repository/org/springframework/spring-context-support/5.1.3.RELEASE/spring-context-support-5.1.3.RELEASE.jar crunchify.com.tutorials.CrunchifyRandomNumber
Crunchify Thread 2 random result: Twitter
Crunchify Thread 1 random result: 98.03486
Crunchify Thread 2 random result: Google
Crunchify Thread 1 random result: 57.48709
Crunchify Thread 2 random result: Google
Crunchify Thread 1 random result: 87.54976
Crunchify Thread 2 random result: Google
Crunchify Thread 1 random result: 75.2223
Crunchify Thread 2 random result: Facebook
Crunchify Thread 1 random result: 91.617584
Crunchify Thread 2 random result: Facebook
Crunchify Thread 1 random result: 64.705574
Crunchify Thread 2 random result: Google
Crunchify Thread 1 random result: 72.043884
Crunchify Thread 2 random result: Google
Crunchify Thread 1 random result: 70.38615
Crunchify Thread 2 random result: Facebook
Crunchify Thread 1 random result: 55.705227
Crunchify Thread 2 random result: Facebook
Crunchify Thread 1 random result: 76.36104
Crunchify Thread 2 random result: Twitter
Crunchify Thread 1 random result: 75.84907

Process finished with exit code 130 (interrupted by signal 2: SIGINT)

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

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