/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package simulacion;
import java.text.DecimalFormat;
/**
*
* @author m
*/
public class MetodoMontecarlo {
public static void main(String[] args) {
MetodoMontecarlo m=new MetodoMontecarlo();
for(int x=0;x<=100;x++){
System.out.println("Numero seudoaleatorios "+x+" -----> "+m.Metodo());
}
}
public int Metodo(){
int respuesta=0;
double x=Math.random();
DecimalFormat d=new DecimalFormat("#.000");
String n=d.format(x);
float c=Float.parseFloat(n);
if(c<=0 || c<=0.125){
respuesta=1;
}else if(c<=0.125 || c<=0.250){
respuesta=2;
}else if(c<=0.250 || c<=0.375){
respuesta=3;
}else if(c<=0.375 || c<=0.5){
respuesta=4;
}else if(c<=0.5 || c<=0.625){
respuesta=5;
}else if(c<=0.625 || c<=0.750){
respuesta=6;
}else if(c<=0.750 || c<=0.875){
respuesta=7;
}else if(c<=0.875 || c<=1){
respuesta=8;
}
return respuesta;
}
}
Método Mixto
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package simulacion;
/**
*
* @author m
*/
public class MetodoMixto {
public static void main(String[] args) {
int xx=8;
MetodoMixto m=new MetodoMixto();
for(int y=0;y<=100;y++){
xx=m.Metodo(xx);
System.out.println("Numero seudoaleatorios "+y+" -----> "+m.Metodo(xx));
}
}
public int Metodo(int y){
int a=3,x,xx;
int c=6;
int m=7;
int xn=y;
x=((a*(xn))+c);
xx=x%m;
return xx;
}
}
No hay comentarios.:
Publicar un comentario