Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
cómo generar la matriz contiene un número aleatorio en orden ascendente usando java

Me gustaría hacer que la matriz contenga números aleatorios en orden ascendente, por ejemplo (1,5,10...100), pero no (5,1,10...). este código pero cuando se ejecuta el número no es ascendente. cual es el error

 Random r=new Random(); int t=10; int a[]=new int[t]; int count=0; int end=0; int curr=0; while(count<t){ curr=r.nextInt(); end=end+curr; count++;} for(int i=0;i<to;i++){ a[i]=r.nextInt(10);} ```
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede usar el método Random#ints que devuelve una secuencia de valores int aleatorios. Suponiendo que desea 10 entradas aleatorias del rango [1,100) ordenadas en orden ascendente:

 Random r = new Random(); int t = 10; int a[] = r.ints(1, 100).distinct().limit(t).sorted().toArray(); System.out.println(Arrays.toString(a));

puede omitir distinct si desea permitir duplicados

Para ordenar en orden descendente, debe colocar los tipos primitivos en Integer y usar Comparator.reverseOrder() o Collections.reverseOrder() y desempaquetarlos después de ordenarlos para almacenarlos en una matriz int

 int a[] = r.ints(0, 100) .distinct() .limit(t) .boxed() .sorted(Comparator.reverseOrder()) .mapToInt(Integer::intValue) .toArray();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!