dimanche 6 février 2022

How to use an existing Java coding of a Monte Carlo to evaluate an integral of single variable and multi variables of any selected function?

So, I already have this Java coding implementing the Monte Carlo simulation and linear congruence method in generating random numbers. Do you have any idea on how to add the coding for the evaluation of an integral of single variable and multi variables of any selected function? I tried a lot of modifications on the coding but all of it did not work. Please help.. This is my first time using Java programming language so I really have no idea what else to do. Here's the link to the question if you need more details.

//Program to generate 1000 integers between 1 and 5
public class Random
{
public static void main(String[] args)
{
//Declare variables to store all generated random numbers
int[] randno=new int[1024];
//Declare variables for seed value, increment, multiplier and modulus
int seed=24, a=9, b=1, c=1024;
//Declare variables to keep track of number of occurrences of 1, 2, 3, 4 and 5
int first=0, second=0, third=0, fourth=0, fifth=0;
//Use linear congruence method to run for interval 0 to 1024
for(int i=0;i<1024;i++)
{
//Check for seed value
if(i==0)
{
//Use linear congruence method for seed value
randno[0]=(a*(seed)+b)%c;
}
else
{
//Use linear congruence method for other 1023 values
randno[i]=(a*(randno[i-1])+b)%c;
}
}
//Use for loop to reset the elements of randno array
for(int i=0;i<1024;i++)
{
//Use if statement to check i^th element of array is smaller than 220 or not
if(randno[i]<220)
{
//Change the value of randno to 1
randno[i]=1;
}
//Use if statement to check i^th element of array is smaller than 370 or not
else if(randno[i]<370)
{
//Change the value of randno to 2
randno[i]=2;
}
//Use if statement to check i^th element of array is smaller than 680 or not
else if(randno[i]<680)
{
//Change the value of randno to 3
randno[i]=3;
}
//Use if statement to check i^th element of array is smaller than 940 or not
else if(randno[i]<940)
{
//Change the value of randno to 4
randno[i]=4;
}
//Use if statement to check i^th element of array is smaller than 1000 or not
else if(randno[i]<1000)
{
//Change the value of randno to 5
randno[i]=5;
}
else
//Change the value of randno to 20 for the remaining numbers
{
randno[i]=20;
}
}
//Use for loop to count the occurrences of 1, 2, 3, 4 and 5
for(int i=0;i<1024;i++)
{
//Check if randno is equal to 1 and then increase the counter first
if(randno[i]==1)
first++;
//Check if randno is equal to 2 and then increase the counter second
if(randno[i]==2)
second++;
//Check if randno is equal to 3 and then increase the counter third
if(randno[i]==3)
third++;
//Check if randno is equal to 4 and then increase the counter forth
if(randno[i]==4)
fourth++;
//Check if randno is equal to 5 and then increase the counter fifth
if(randno[i]==5)
fifth++;
}
//Print the percentage of 1, 2, 3, 4 and 5
System.out.println("Percent of 1: " +Math.ceil(((first/1024.0)*100)));
System.out.println("Percent of 2: " +Math.ceil(((second/1024.0)*100)));
System.out.println("Percent of 3: " +Math.ceil(((third/1024.0)*100)));
System.out.println("Percent of 4: " +Math.ceil(((fourth/1024.0)*100)));
System.out.println("Percent of 5: " +Math.ceil(((fifth/1024.0)*100)));
}
}



Aucun commentaire:

Enregistrer un commentaire