C Program to Generate Random Numbers using Probability Distribution Function

«
»
This is a C Program to generate random numbers using Probability Distribution Function.The probability density function of a continuous random variable is a function which can be integrated to obtain the probability that the random variable takes a value in a given interval.

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

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6.  
  7. //This is a sample program to generate a random numbers based on probability desity function of spiner
  8. //pdf(x) = 1 if x>360
  9. //       = 0 if x<0
  10. //       = x/360 otherwise
  11. int N = 10;
  12. int main(int argc, char **argv) {
  13.     int p = 0, i;
  14.     for (i = 0; i < N; i++) {
  15.         p = rand() % 400;
  16.         if (p > 360)
  17.             printf("%d ", 0);
  18.         else if (p < 0)
  19.             printf("%d ", 0);
  20.         else
  21.             printf("%f ", p * 0.1 / 360);
  22.  
  23.     }
  24.     printf("...");
  25. 	return 0;
  26. }

Output:

advertisement
$ gcc ProbabilityDist.c
$ ./a.out
 
0.011389 0.018611 0.092778 0.027778 0 0.034444 0.077222 0.043889 0.045000 0.017778 ...

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

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

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 & technical discussions at Telegram SanfoundryClasses.