简体   繁体   中英

function to generate and display N random numbers between 0 and 1

I have to generate randoms numbers, i have a code running and working, but it doesnt seems random, it generates sequences, is right anyway? or is there another way to do this

public static void main(String[] args) {

    Scanner teclado = new Scanner(System.in);
    System.out.println("Ingrese el numero N");
    int n = teclado.nextInt();

    for (int i = 1; i <= n; i++){

        double d = numerosAleatorios(i,n);
        System.out.println(d);
    }

}

public static double numerosAleatorios(double b, double c){

     return  b / c;
}

Math.random() returns a pseudorandom double greater than or equal to 0.0 and less than 1.0.

public static void main(String[] args) {

    Scanner teclado = new Scanner(System.in);
    System.out.println("Ingrese el numero N");
    int n = teclado.nextInt();
    for (int i = 1; i <= n; i++){
        double d = Math.random();
        System.out.println(d);
    }
}

Ingrese el numero N

8

0.660560867388863

0.6664125435482811

0.4814731375341803

0.040360135942367203

0.5503428291806611

0.25928232439131915

0.8536945699105207

0.41418876402957383

它不是随机的,因为变量i加1并除以一个常数(N),因此对于每个N,它将显示相同的输出,我建议更改代码以独立于此类变量,以实现真正的随机性

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM