4
package generadordenumerosaleatoriosmixto; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.LinkedList; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; //import java.util.logging.Level; //import java.util.logging.Logger; import javax.swing.JOptionPane; /** * * @author x */ public class GenMixto { /** * @param args the command line arguments */ static void metodoMixto(int x, int a, int c, int mod, PrintWriter pw) { int n = 0; int semilla = 0; int xn1; LinkedList<Integer> xxn1 = new LinkedList(); LinkedList<Integer> xx = new LinkedList(); LinkedList<Integer> nn = new LinkedList(); while(semilla != x) { if(n == 0) { semilla = x; } nn.add(n); xx.add(x); //System.out.print(n + "\t" + x + "\t"); xn1 = (a * x + c) % mod; //System.out.print(xn1 + "\n"); x = xn1; n = n + 1; //System.out.println(n + "\t" + x + "\t" + xn1); xxn1.add(xn1); }

Generador de números pseualeatorios mixto de simulación en Java netbeans

Embed Size (px)

Citation preview

Page 1: Generador de números pseualeatorios mixto de simulación en Java netbeans

package generadordenumerosaleatoriosmixto; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.LinkedList; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; //import java.util.logging.Level; //import java.util.logging.Logger; import javax.swing.JOptionPane; /** * * @author x */ public class GenMixto { /** * @param args the command line arguments */ static void metodoMixto(int x, int a, int c, int mod, PrintWriter pw) { int n = 0; int semilla = 0; int xn1; LinkedList<Integer> xxn1 = new LinkedList(); LinkedList<Integer> xx = new LinkedList(); LinkedList<Integer> nn = new LinkedList(); while(semilla != x) { if(n == 0) { semilla = x; } nn.add(n); xx.add(x); //System.out.print(n + "\t" + x + "\t"); xn1 = (a * x + c) % mod; //System.out.print(xn1 + "\n"); x = xn1; n = n + 1; //System.out.println(n + "\t" + x + "\t" + xn1); xxn1.add(xn1); }

Page 2: Generador de números pseualeatorios mixto de simulación en Java netbeans

if(n == mod && x == semilla) { for (int i = 0; i < mod; i++) { System.out.println(nn.get(i) + "\t" + xx.get(i) + "\t" + xxn1.get(i)); } System.out.println("El periodo es completo: "+ n); pw.print("x0 = " + x + ", a = " + a + ", c = " + c + ", mod = " + mod); pw.println(); } else { //System.out.println("El periodo es incompleto: "+ n); } //System.out.println("Semilla: "+ semilla); } public static void main(String[] args) throws IOException { Scanner scan = new Scanner(System.in); System.out.println("Introduce el valor de b: "); int b = scan.nextInt(); while(b<5 && b>63) { System.out.println("Introduce de nuevo el valor de b: debe ser entre 5 y 63: "); b = scan.nextInt(); } int m = (int)Math.pow(2, b); System.out.println("Introduce el valor de la semilla(x0): "); int x = scan.nextInt(); while(x>m) { System.out.println("Introduce el valor de la semilla(x0) de nuevo, debe ser menor que "+m+": "); x = scan.nextInt(); } System.out.println("n"+ "\t" + "X0" + "\t" + "Xn+1"); File archivo = new File("resultados.txt"); FileWriter fw; BufferedWriter bf; PrintWriter pw; try { fw = new FileWriter(archivo); bf = new BufferedWriter(fw); pw = new PrintWriter(bf); try { if(archivo.createNewFile())

Page 3: Generador de números pseualeatorios mixto de simulación en Java netbeans

{ System.out.println("Se ha creado el archivo"); } } catch (IOException ex) { JOptionPane.showMessageDialog(null, ex); } LinkedList<Integer> aa = new LinkedList(); LinkedList<Integer> cc = new LinkedList(); for (int i = 0; i < m; i++) { if((i % 2 != 0 )&& (i % 3 != 0 && i % 5 != 0)) { aa.add(i); } if((i % 2 != 0 )&& (i%m==5)) { cc.add(i); } } // for (int j = 0; j < m; j++) { // if((j % 2 != 0 )&& (j%m==5)) // { // cc.add(j); // } // } for (int k = 0; k < aa.size(); k++) { for (int l = 0; l < cc.size(); l++) { metodoMixto(x, aa.get(k), cc.get(l), m, pw); } } pw.close(); bf.close(); fw.close(); } catch (IOException ex) { Logger.getLogger(GenMixto.class.getName()).log(Level.SEVERE, null, ex); } // System.out.println(""); // System.out.println("Introduce el valor del elemento multiplicativo(a): "); // int a = scan.nextInt(); // while(!(a % 2 == 0 )&& (a % 3 != 0 || a % 5 != 0)) // { // System.out.println("Introduce el valor del elemento multiplicativo(a) por favor: "); // a = scan.nextInt(); // } // System.out.println(""); // System.out.println("Introduce el valor del modulo(m)");

Page 4: Generador de números pseualeatorios mixto de simulación en Java netbeans

// int m = scan.nextInt(); // System.out.println("Introduce el valor del elemento aditivo(c): "); // int c = scan.nextInt(); // while(a % 2 != 0 || c % m == 5) // { // System.out.println("Introduce de nuevo el valor del elemento aditivo(c) por favor: "); // c = scan.nextInt(); // } } }