Java examples for java.util:Random
get Random File
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashSet; import java.util.Set; public class Main{ public static File getRandomFile() throws IOException { File file = File.createTempFile("randomDataUtil", "tmp"); file.deleteOnExit();//from w w w. j a v a 2s. c om OutputStream out = new FileOutputStream(file); Writer writer = null; try { writer = new OutputStreamWriter(out, "UTF-8"); writer.write(getRandomWords(RandomDataUtil .getRandomInt(10, 500))); return file; } finally { if (writer != null) { try { writer.close(); } catch (Exception e) { } } } } public static String getRandomWords(int wordCount) { StringBuilder buf = new StringBuilder(); for (int i = 0; i < wordCount; i++) { if (i > 0) { buf.append(" "); } String word = RandomDataUtil .getRandomAlphaString(RandomDataUtil .getRandomInt(5, 15)); if (i == 0) { word = Character.toUpperCase(word.charAt(0)) + word.substring(1).toLowerCase(); } else { word = word.toLowerCase(); } buf.append(word); } return buf.toString(); } public static String getRandomWords(int min, int max) { int wordsToCreate = getRandomInt(min, max); return getRandomWords(wordsToCreate); } public static int getRandomInt() { return getRandomInt(1, Integer.MAX_VALUE); } public static int getRandomInt(int min, int max) { return min + (int) ((Math.random() * (max - min))); } public static String getRandomAlphaString(int length) { StringBuilder b = new StringBuilder(); for (int i = 0; i < length; i++) { // ascii 60 to 90 char str = (char) (65 + (int) (Math.random() * (90 - 65))); b.append(str); } return b.toString(); } }