Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
Matriz aleatoria de una matriz - Java

Tengo una matriz: {rojo, azul, verde}. Quiero generar otra matriz que tenga contenido aleatorio, por ejemplo: {red,red,blue,green,blue} . Quiero usar una longitud variable de la matriz aleatoria.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

puedes intentar algo como esto:

 import java.util.ArrayList; import java.util.Random; public class RandomArrayTest { public static void main(String[] args) { System.out.println(RandomArrayTest.randomArrayOfColors(10)); // for example } public static ArrayList<String> randomArrayOfColors(int lenOfArray){ String[] colors = {"RED", "GREEN", "BLUE"}; ArrayList<String> rndArray = new ArrayList<String>(); Random rnd = new Random(); for(int i=0; i<lenOfArray; i++){ // populate the array rndArray.add(colors[rnd.nextInt(colors.length)]); } return rndArray; } }

La salida por ejemplo:

 [GREEN, GREEN, BLUE, GREEN, RED, BLUE, GREEN, RED, RED, BLUE]
over 4 years ago · Santiago Trujillo Denunciar

0

Aquí hay un ejemplo de cómo puede usar random con array :

 public static void main(String[] args) { String colors[] = {"red", "blue", "green"};//your array of colors int n = 10;//number of element you want to make in your new array String random[] = new String[n]; for(int i = 0; i< n; i++){ //add values with random values of the colors array random[i] = colors[new Random().nextInt(colors.length)]; } System.out.println(Arrays.toString(random));//print the random array }

Producción

 [red, green, green, green, green, green, red, blue, red, red]
over 4 years ago · Santiago Trujillo Denunciar

0

Pseudocódigo:

 colours <- {red, blue, green} length <- random (0 to maxLength) repeat length times colour <- pick random from colours append colour to outputArray end repeat return outputArray

¿Fue realmente tan difícil?

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda