GENERATE RANDOM NUMBERS IN A VECTOR N JAVA

-2

fellow nights, I'm learning programming, so I'm struggling with something. My problem is that I have a vector and I ask for numbers.

But my current problem is that for example if I want to ask for 1000 numbers to my program, I want you to give me random numbers from 0 to 1000,

or if I insert 10,000 numbers that give me those from 0 to 10,000

if there are 150,000 thousand that I give from 0 to that rank etc etc ..

How could I do that with the Randon, I already tried but it does not work for me, thanks and regards!

    
asked by Brooke 9 13.12.2018 в 09:06
source

1 answer

0

here is the code, I have decided to use arrarys of type [] because if from the beginning you will know its size because it will be a bit more efficient. Any questions comment my answer

import java.io.BufferedReader;
import java.io.InputStreamReader;


public class Stack_generar_random_en_vector {

    public static void main(String[] args) {
        int tamañoArray = 0;
        int[] array  = null;

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

            System.out.println("Introduce el tamaño que quieres que tenga el array");
            System.out.print("->");
            //este sera el tamaño del array
             tamañoArray = Integer.parseInt(br.readLine());
             array = new int[tamañoArray];
            //definamos ahora los posibles numeros randoms

            int M = 0; //minimo posible
            int N = tamañoArray ; //maximo posible

            //ahora rellenamos el array con esos numeros aleatorios
            for (int i = 0; i < array.length; i++) {
                int valorAleatorio = (int) Math.floor(Math.random() * (N - M + 1) + M);  // Valor entre M y N, ambos incluidos.
                array[i] = valorAleatorio;

            }

        } catch (Exception e) {
            System.out.println("Error "+e);
        }


        //ahora hacemos display de los valores para comprobar que haya funcionado

        for (int i = 0; i < array.length; i++) {

            System.out.println(array[i]);
        }
    }

}
    
answered by 14.12.2018 в 15:16