I need help writing a function in java for the following Wri
I need help writing a function in java for the following:
Write a function to generate random gaussian numbers with a specified mean and standard distribution. As an example if I wanted to generate random numbers with a mean of 70 and a standard deviation of .5, I could code double gausRand = myRand.nextGaussian() *5 + 70. For this function you should treat a test as true if it returns a value within a specified range and false otherwise. For example, if my function were calculating gaussian values with a mean of 70 and a standard deviation of .5, it would return true if the number generated were 70.43 but would return false if the number generated were 70.51. Your function should be able to run n number of times and give the % accuracy after being run n number of times.
I will be using this to run various test to determine how many times I need to excute the code to be within 1% of the number and also .1%
Thanks
Solution
import java.util.*;
public class HelloWorld{
//variable to hold number of attempts
static int count=0;
//function to check if genertaed number is within the range
public static boolean isGaussian()
{
//increment the counter
count++;
//initialize guasian random number
Random myRand=new Random();
double gausRand = myRand.nextGaussian() *5 + 70;
System.out.print(gausRand);
//check if the number is in range
if(gausRand-70 >=0 && gausRand-70<=0.5)
return true;
else
return false;
}
//main function to trigger
public static void main(String []args){
//variable to hold correct times
int correct=0;
//run for 10 times
for(int i=0;i<10;i++)
{
if(isGaussian())
{
correct++;
System.out.println(\" is a gaussian\");
}
else
System.out.println(\" is not gaussian\");
}
//display the % of accuracy
System.out.println(\"% of accuracy is: \"+(100*correct)/count);
}
}
Sample Output:
74.69503657642234 is not gaussian
70.19444031941912 is a gaussian
70.25786184531226 is a gaussian
69.05996072295738 is not gaussian
66.35345301425204 is not gaussian
69.10819841157641 is not gaussian
67.25230116108068 is not gaussian
68.43653873377706 is not gaussian
71.34828844657127 is not gaussian
72.08276324247845 is not gaussian
% of accuracy is: 20

