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.

  1. //This is a sample program to generate a random numbers based on probability desity function of spiner 
  2. //pdf(x) = 1 if x>360
  3. //       = 0 if x<0
  4. //       = x/360 otherwise
  5. import java.util.Random;
  6.  
  7. public class Probability_distribution_Function_Random_Numbers 
  8. {
  9.     static int N = 10;
  10.     public static void main(String args[])
  11.     {
  12.         Random random = new Random();
  13.         int p=0;
  14.         for(int i=0; i<N; i++)
  15.         {
  16.             p = random.nextInt(400);
  17.             if(p > 360)
  18.                 System.out.println(1 + " ");
  19.             else if(p < 0)
  20.                 System.out.println(0 + " ");
  21.             else
  22.                 System.out.println(p*0.1/360 + " ");
  23.         }			
  24.     }
  25. }

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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.