Está en la página 1de 4

import java.util.

ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
* Simulación: Congruencial.
*/
public class Congruencial {

public static void main(String[] args) {


int a = 0, x = 0,semilla;
int opcion = 0;
ArrayList<Integer> valores = new ArrayList<>();

Scanner E = new Scanner(System.in);


boolean bandera = true, aux = false;
System.out.println("Simulador: Congruencial (Sistema Binario).");
do {
////////////////////////////////////////////////////
System.out.println(" /////////////////////////////////////////");
do {
System.out.println("Ingresa el valor de a (impar, no divisible entre 3 o 5)");
a = E.nextInt();
if (a % 2 == 0 || a % 3 == 0 || a % 5 == 0) {
System.out.println("El valor de 'a' debe ser no divisible entre 3 o 5, además debe ser impar.");
} else {
aux = true;
}
} while (aux == false);

/////////////////////////////////////////////////
System.out.println(" /////////////////////////////////////////");
do {
System.out.println("Tienes dos opciones para definir el valor de x."
+ "\n1.Introducir el valor de x por teclado.\n2.Generar un número random.");
int op = E.nextInt();
switch (op) {
case 1:
System.out.println("Ingresa el valor de X (mayor o igual a 0)");
x = E.nextInt();
if (x < 0) {
System.out.println("El valor de X no debe ser menor que 0");
} else {
aux = false;
}
break;
case 2:
Random R = new Random();
semilla = R.nextInt(9999) + 1;
aux = false;
break;
default:
break;
}
} while (aux == true);

/////////////////////////////////////////
System.out.println(" /////////////////////////////////////////");
int mod = 0;
do {
System.out.println("Tienes dos opciones para definir el valor de m."
+ "\n1.Generar valor de m al azar.\n2.Numero primo mayor y menor que p^d-1.");
int op = E.nextInt();
switch (op) {
case 2:
System.out.println("Ingresa el valor de m");
mod = E.nextInt();
if (String.valueOf(mod).length() > 10) {
System.out.println("El valor de 'm' es mayor de p^d-1");
} else {
aux = true;
}
break;
case 1:
Random R = new Random();
mod = R.nextInt(9999) + 1;
if (String.valueOf(mod).length() > 10) {
System.out.println("El valor de 'm' es mayor de p^d-1");
} else {
aux = true;
}
break;
default:
System.out.println("Ingresa una opción válida.");
break;
}
} while (aux == false);
/////////////////////////////////////////
System.out.println(" /////////////////////////////////////////");
int cons;
do {
System.out.println("Ingresa el valor de c");
cons = E.nextInt();
if (cons % 8 != 5) {
System.out.println("Ingresa otro valor, su residuo al dividirlo entre 8 debe ser 5");
} else {
aux = false;
}
} while (aux == true);

//////////////////////////////////////////////////////
//Calculo Congruencial
int i = 0;
System.out.println("\nN°\t\tX\t\taXn+c\t\t(aXn+c)/m\t\tXn+1");
valores.add(x);
do {
int op1 = (a * x) + cons;
int op2 = ((a * x) + cons) % mod;
System.out.println((i + 1) + "\t\t" + x + "\t\t" + op1 + "\t\t" + ((a * x) + cons) + "/" + mod + "\t\t\t" +
op2);
if (valores.contains(op2)) {
x = op2;
op1 = (a * x) + cons;
op2 = ((a * x) + cons) % mod;
System.out.println((i + 1) + "\t\t" + x + "\t\t" + op1 + "\t\t" + ((a * x) + cons) + "/" + mod + "\t\t\t"
+ op2);
System.out.println("Valores de X repetidos. Simulacion terminada");
aux = true;
bandera = false;
} else {
valores.add(op2);
x = op2;
}
i++;
} while (aux == false);
} while (bandera);
}
}

También podría gustarte