Created
October 5, 2010 22:38
-
-
Save lfborjas/612478 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*Programa que genera un arreglo con tamaño aleatorio de enteros aleatorios | |
e imprime un histograma de frecuencias | |
*/ | |
package ejercicios; | |
//clase de java para generar números aleatorios: | |
import java.util.Random; | |
//sirve para representar colecciones tabulares | |
import java.util.HashMap; | |
//contiene distintas utilidades para colecciones | |
import java.util.Collections; | |
//para poder iterar sobre el mapa | |
import java.util.Map.Entry; | |
import java.util.Arrays; | |
public class Histograma{ | |
private static String repeat(String what, int howMany){ | |
StringBuilder buf = new StringBuilder(); | |
for(int i=0; i<howMany; i++){ | |
buf.append(what); | |
} | |
return buf.toString(); | |
} | |
public static void main (String [] args){ | |
Random generator = new Random(); | |
//genera números aleatorios entre 0 y 20 | |
int[] nums = new int[generator.nextInt(20)]; | |
for(int i=0; i< nums.length; i++){ | |
//números aleatorios entre 0 y 10 | |
nums[i] = generator.nextInt(10); | |
} | |
//creamos una tabla que guarde el número y su cantidad de ocurrencias | |
//no se pueden usar tipos primitivos en este tipo de estructuras | |
HashMap<Integer, Integer> tabla = new HashMap<Integer, Integer>(); | |
System.out.printf("Números generados: \n %s\n", Arrays.toString(nums)); | |
for(int num: nums){ | |
Integer index = new Integer(num); | |
if(!tabla.containsKey(index)) | |
tabla.put(index, new Integer(1)); | |
else | |
tabla.put(index, tabla.get(index)+1); | |
} | |
for(Entry<Integer, Integer> fila: tabla.entrySet()){ | |
System.out.printf("%s: %s\n",fila.getKey(), repeat("*", fila.getValue().intValue())); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**Muestra de cómo generar "palabras" al azar*/ | |
package ejercicios; | |
import java.util.Random; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
public class RandWords{ | |
private static String genPalabra(){ | |
Random gen = new Random(); | |
//la palabra más larga en español [dicen en internerd] tiene 27 letras | |
byte[] bytes = new byte[gen.nextInt(27)]; | |
//llenar el arreglo con bytes aleatorios | |
//gen.nextBytes(bytes); | |
for(int i = 0; i< bytes.length; i++){ | |
//el byte tiene que estar en el rango [97,122] (letras minúsculas) | |
//para generar aleatorios en un rango: (fin - inicio +1) * gen.nextDouble() + inicio; | |
bytes[i] = (byte) (26 * gen.nextDouble() + 97); | |
} | |
return new String(bytes); | |
} | |
public static void main(String [] args){ | |
//instanciar el generador de aleatorios | |
Random gen = new Random(); | |
//obtener el número de palabras de la línea de comandos: | |
int numPalabras = args.length == 1 ? Integer.parseInt(args[0]) : gen.nextInt(20); | |
//guardar las palabras en un ArrayList: | |
ArrayList<String> palabras = new ArrayList<String>(); | |
//llenar el arrayList con n palabras al azar: | |
for(int i=0; i<numPalabras; i++){ | |
palabras.add(genPalabra()); | |
} | |
//ordenar el ArrayList e imprimirlo | |
Collections.sort(palabras); | |
System.out.printf("La lista ordenada lexicográficamente: \n %s \n", palabras.toString().replace(",", "\n")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment