Using the Rejection Method code (preferably in Python) a program to generate ran
ID: 3738461 • Letter: U
Question
Using the Rejection Method code (preferably in Python) a program to generate random variables X with distribution determined by the density function f(x) 1/3 (x-1)2 on the interval [0,3]. Write a subroutine that implements steps 1,2,3,4 of Algorithm then call it at least 10,000 times. Make a table of values 0, 0.1, 0.2, 2.8, 2.9 and keep a running tally of the number of variables that fall within each interval. At the end of the program, print out a histogram showing how the values accumulated. It should look like the graph presented in class.Explanation / Answer
It will be really hard to plot a histogram in c but with this code you will be able to plot it very easily by converting it to python.
Store all values of all runtimes and plot it via some ploating library like matplotlib.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float surand()
{
return( (float) rand()/RAND_MAX );
}
float urand(float low, float high)
{
return(low+(high-low)*surand());
}
float generatez()
{
float x,y,fx;
while (1)
{
x=urand(0,1);
y=urand(0,3);
fx=0.33*(x-1)*(x-1);
if (y<fx)
{
break;
}
}
return(x);
}
void main(int argc, char *argv[])
{
int i,n;
float z;
n=atoi(argv[1]);
for (i=0;i<n;i++)
{
z=generatez();
printf(" %2.4f ",z);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.