Java Program to Generate Random Numbers Using Probability Distribution Function

This is a java program to generate random numbers using a probability distribution. Probability distribution is based on probability density function. a probability density function (pdf), or density of a continuous random variable, is a function that describes the relative likelihood for this random variable to take on a given value. The probability of the random variable falling within a particular range of values is given by the integral of this variable’s density over that range—that is, it is given by the area under the density function but above the horizontal axis and between the lowest and greatest values of the range.

Here is the source code of the Java Program to Generate Random Numbers Using Probability Distribution Function. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

//This is a sample program to generate a random numbers based on probability desity function of spiner 
//pdf(x) = 1 if x>360
//       = 0 if x<0
//       = x/360 otherwise
import java.util.Random;
 
public class Probability_distribution_Function_Random_Numbers 
{
    static int N = 10;
    public static void main(String args[])
    {
        Random random = new Random();
        int p=0;
        for(int i=0; i<N; i++)
        {
            p = random.nextInt(400);
            if(p > 360)
                System.out.println(1 + " ");
            else if(p < 0)
                System.out.println(0 + " ");
            else
                System.out.println(p*0.1/360 + " ");
        }			
    }
}

Output:

$ javac Probability_distribution_Function_Random_Numbers.java
$ java Probability_distribution_Function_Random_Numbers
The random numbers are:
0.08527777777777779 
0.07361111111111111 
0.007222222222222223 
0.08694444444444445 
1 
1 
0.05527777777777779 
0.0952777777777778 
0.04888888888888889 
0.016944444444444446

Related posts:

A Guide to Concurrent Queues in Java
Apache Commons Collections MapUtils
Java Program to Implement Stein GCD Algorithm
A Guide to Java HashMap
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Program to Implement Self organizing List
Guava – Join and Split Collections
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Java Program to Solve any Linear Equation in One Variable
Hướng dẫn Java Design Pattern – Flyweight
Java Program to Implement Park-Miller Random Number Generation Algorithm
Lớp Collectors trong Java 8
Debug a HttpURLConnection problem
Java – Convert File to InputStream
The HttpMediaTypeNotAcceptableException in Spring MVC
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Java Program to Implement Fermat Factorization Algorithm
Java Program to Implement Min Heap
Getting Started with Forms in Spring MVC
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Introduction to Netflix Archaius with Spring Cloud
Java – Reader to String
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Java Program to Implement Hash Tables Chaining with List Heads
Java Program to Implement Repeated Squaring Algorithm
An Intro to Spring Cloud Security
Java Program to Implement Find all Back Edges in a Graph
Java Program to Implement Knapsack Algorithm
Java Program to Implement Queue using Linked List
Java Program to Implement LinkedList API
Custom Cascading in Spring Data MongoDB
Entity To DTO Conversion for a Spring REST API