mercredi 16 octobre 2019

how random generate in java?

is a method of NON-CONGRUENTIAL GENERATOR of random numbers,two seeds are multiplied, then the center is calculated,the center is reduced to 3 digits and that is the random number generated. the second seed then becomes the first seed and the center becomes the second seed.

the program must generate 100 random numbers, but because it does not make the correct calculations try to divide it so that it works well but miscalculates the numbers, only generated well until the 4th iteration.

Middle Product code

     package productomedio;
     import java.util.Scanner;
     public class Productomedio {
     public static String  snumero4;
     public static long numero1,numero2,numero7,numero4,  numero8;
    public static long[]numero6 ;
    public static void main(String[] args) {
    Scanner entrada = new Scanner(System.in);
    String semilla,semilla2, snumero2, snumero3,snumero5, snumero6,snumero7;
    int tam1, tam2,tam3,  i, primerc;
    long   numero3, numero5, numero9,numero10;
    System.out.println("Escriba semilla 1: ");
   semilla = entrada.next();
   tam1 = semilla.length();
   System.out.println("Escriba semilla 2: ");
   semilla2 = entrada.next();
   tam2 = semilla2.length();
   if(tam1==tam2){
   System.out.println("Cantidad de digitos:  " + tam1);
   numero1 = Integer.parseInt(semilla);
   numero2 = Integer.parseInt(semilla2);
   numero6=new long[101];
   for (i=1; i<=2; i++){
   numero3 = numero1*numero2;
   snumero3 = Long.toString(numero3);
   tam3 = snumero3.length();
   primerc = (tam3-tam1) / 2;
   snumero4 = snumero3.substring(primerc,primerc+tam1);
   numero4=Integer.parseInt(snumero4);
   numero6 [i]= numero4;
   System.out.println(i + ". " + snumero4);
   numero1 = Integer.parseInt(snumero4);
   }
   for (i=3; i==3; i++){   
   numero5 = numero6[1]*numero6[2];      
   snumero5 = Long.toString(numero5);      
   tam3 = snumero5.length();
   primerc = (tam3-tam1) / 2;
   snumero6 = snumero5.substring(primerc,primerc+tam1);
   numero8=Integer.parseInt(snumero6);
   numero6 [i]= numero8;
   System.out.println(i + ". " + snumero6);
   numero1 = numero6[3];
   } 
   numero2 = numero6[2];
   for (i=4; i<=6; i++){
   numero9= numero6[i-1];
   System.out.println(i + ". " + numero9);
   System.out.println(i + ". " + numero2);
   numero5 = numero6[i-1]*numero2;
   snumero5 = Long.toString(numero5);
   tam3 = snumero5.length();
   primerc = (tam3-tam1) / 2;
   snumero6 = snumero5.substring(primerc,primerc+tam1);
   numero10=Integer.parseInt(snumero6);
   numero2=numero10;
   numero6 [i]= numero8;
   System.out.println(i + ". " + snumero6);
   }
   }
   }
   }

the program must generate the same values of the seeds and the numbers generated with excel. file excel




Aucun commentaire:

Enregistrer un commentaire