List of usage examples for java.util Random nextInt
public int nextInt(int bound)
From source file:FileList.java
public static void main(String[] args) { final int assumedLineLength = 50; File file = new File(args[0]); List<String> fileList = new ArrayList<String>((int) (file.length() / assumedLineLength) * 2); BufferedReader reader = null; int lineCount = 0; try {//from ww w. j ava2 s . co m reader = new BufferedReader(new FileReader(file)); for (String line = reader.readLine(); line != null; line = reader.readLine()) { fileList.add(line); lineCount++; } } catch (IOException e) { System.err.format("Could not read %s: %s%n", file, e); System.exit(1); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { } } } int repeats = Integer.parseInt(args[1]); Random random = new Random(); for (int i = 0; i < repeats; i++) { System.out.format("%d: %s%n", i, fileList.get(random.nextInt(lineCount - 1))); } }
From source file:Main.java
public static void main(String[] args) { long[] numbers = new long[] { 1, 2, 3, 4, 5 }; Random id = new Random(numbers.length); String[] name = new String[] { "a", "b", "c", "d", "e" }; for (int i = 0; i < numbers.length; i++) { int randomPosition = id.nextInt(4); long temp = numbers[i]; numbers[i] = numbers[randomPosition]; numbers[randomPosition] = temp;//from w w w . ja va 2 s .c o m } for (int i = 0; i < name.length; i++) { int randomPosition = id.nextInt(4); String temp = name[i]; name[i] = name[randomPosition]; name[randomPosition] = temp; } for (int i = 0; i < numbers.length; i++) { System.out.println(i + " ID = " + numbers[i] + " and name = " + name[i]); } }
From source file:TestCipher.java
public static void main(String args[]) throws Exception { Set set = new HashSet(); Random random = new Random(); for (int i = 0; i < 10; i++) { Point point = new Point(random.nextInt(1000), random.nextInt(2000)); set.add(point);/* w ww . java 2 s . c o m*/ } int last = random.nextInt(5000); // Create Key byte key[] = password.getBytes(); DESKeySpec desKeySpec = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); // Create Cipher Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); desCipher.init(Cipher.ENCRYPT_MODE, secretKey); // Create stream FileOutputStream fos = new FileOutputStream("out.des"); BufferedOutputStream bos = new BufferedOutputStream(fos); CipherOutputStream cos = new CipherOutputStream(bos, desCipher); ObjectOutputStream oos = new ObjectOutputStream(cos); // Write objects oos.writeObject(set); oos.writeInt(last); oos.flush(); oos.close(); // Change cipher mode desCipher.init(Cipher.DECRYPT_MODE, secretKey); // Create stream FileInputStream fis = new FileInputStream("out.des"); BufferedInputStream bis = new BufferedInputStream(fis); CipherInputStream cis = new CipherInputStream(bis, desCipher); ObjectInputStream ois = new ObjectInputStream(cis); // Read objects Set set2 = (Set) ois.readObject(); int last2 = ois.readInt(); ois.close(); // Compare original with what was read back int count = 0; if (set.equals(set2)) { System.out.println("Set1: " + set); System.out.println("Set2: " + set2); System.out.println("Sets are okay."); count++; } if (last == last2) { System.out.println("int1: " + last); System.out.println("int2: " + last2); System.out.println("ints are okay."); count++; } if (count != 2) { System.out.println("Problem during encryption/decryption"); } }
From source file:com.intuit.utils.PopulateUsers.java
public static void main(String[] args) { Date now = new Date(); System.out.println("Current date is: " + now.toString()); MongoClient mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("tweetsdb"); DBCollection collection = db.getCollection("userscollection"); WriteResult result = collection.remove(new BasicDBObject()); int userIndex = 1; for (int i = 1; i <= 10; i++) { JSONObject userDocument = new JSONObject(); String user = "user" + userIndex; userDocument.put("user", user); JSONArray followerList = new JSONArray(); Random randomGenerator = new Random(); for (int j = 0; j < 3; j++) { int followerId = randomGenerator.nextInt(10) + 1; // Assumption here is, a user will not be a follower on himself while (followerId == userIndex) { followerId = randomGenerator.nextInt(10) + 1; }/* ww w . j a va 2 s. c om*/ String follower = "user" + followerId; if (!followerList.contains(follower)) { followerList.add(follower); } } userDocument.put("followers", followerList); JSONArray followingList = new JSONArray(); for (int k = 0; k < 3; k++) { int followingId = randomGenerator.nextInt(10) + 1; // Assumption here is, a user will not be following his own tweets while (followingId == userIndex) { followingId = randomGenerator.nextInt(10) + 1; } String followingUser = "user" + followingId; if (!followingList.contains(followingUser)) { followingList.add(followingUser); } } userDocument.put("following", followingList); System.out.println("Json string is: " + userDocument.toString()); DBObject userDBObject = (DBObject) JSON.parse(userDocument.toString()); collection.insert(userDBObject); userIndex++; } // try { // FileWriter file = new FileWriter("/Users/dmurty/Documents/MongoData/usersCollection.js"); // file.write(usersArray.toJSONString()); // file.flush(); // file.close(); // } catch (IOException ex) { // Logger.getLogger(PopulateUsers.class.getName()).log(Level.SEVERE, null, ex); // } }
From source file:MainClass.java
public static void main(String args[]) { Random randomNumbers = new Random(); // random number generator int face; // stores each random integer generated for (int i = 1; i <= 20; i++) { // pick random integer from 1 to 6 face = 1 + randomNumbers.nextInt(6); System.out.printf("%d ", face); // if i is divisible by 5, start a new line of output if (i % 5 == 0) System.out.println(); }//from w w w. j a v a 2s . co m }
From source file:Main.java
License:asdf
public static void main(String[] args) { int howManyWords = 2; List<String> listOfWords = new ArrayList<>(); Random random = new Random(); listOfWords.addAll(Arrays.asList(randomMessages)); List<String> selectedRandomMessages = new ArrayList<>(); for (int i = 0; i < howManyWords; i++) { int randomNumber = random.nextInt(listOfWords.size()); String randomItem = listOfWords.get(randomNumber); selectedRandomMessages.add(randomItem); listOfWords.remove(randomItem);// w ww. jav a 2 s. c o m } System.out.println(selectedRandomMessages); }
From source file:QuickSort.java
/** * @param args//from w ww . jav a 2 s .c om */ public static void main(String[] args) { Integer[] array = new Integer[15]; Random rng = new Random(); int split = 10; for (int i = 0; i < split; i++) { array[i] = new Integer(rng.nextInt(100)); } for (int i = split; i < array.length; i++) { array[i] = null; } System.out.println(Arrays.toString(array)); QuickSort.sort(array, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1.compareTo(o2); }; }, 0, split - 1); System.out.println(Arrays.toString(array)); }
From source file:MathOps.java
public static void main(String[] args) { // Create a random number generator, // seeds with current time by default: Random rand = new Random(); int i, j, k;/*from w ww . ja v a2 s .c o m*/ // Choose value from 1 to 100: j = rand.nextInt(100) + 1; k = rand.nextInt(100) + 1; printInt("j", j); printInt("k", k); i = j + k; printInt("j + k", i); i = j - k; printInt("j - k", i); i = k / j; printInt("k / j", i); i = k * j; printInt("k * j", i); i = k % j; printInt("k % j", i); j %= k; printInt("j %= k", j); // Floating-point number tests: float u, v, w; // applies to doubles, too v = rand.nextFloat(); w = rand.nextFloat(); printFloat("v", v); printFloat("w", w); u = v + w; printFloat("v + w", u); u = v - w; printFloat("v - w", u); u = v * w; printFloat("v * w", u); u = v / w; printFloat("v / w", u); // the following also works for // char, byte, short, int, long, // and double: u += v; printFloat("u += v", u); u -= v; printFloat("u -= v", u); u *= v; printFloat("u *= v", u); u /= v; printFloat("u /= v", u); }
From source file:apidemo.APIDemo.java
public static void main(String[] args) throws InterruptedException, Exception { Random random = new Random(); int uReqFeq = 1; while (uReqFeq < 101) { String strJSON = ObjectToString(new JSMessage((long) random.nextInt(10) + 1, (long) random.nextInt(10) + 11, "msg_" + Integer.toString(uReqFeq))); String res = sendPostJson("http://localhost:9494/api/nsservice/msg", strJSON, 10000); System.out.println("Result: " + res); ++uReqFeq;//from ww w . j a va 2 s .co m } }
From source file:Main.java
public static void main(String[] args) { int maximum = 100; JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Integer[] oneRow = { 0, 0, 0, 0 }; String[] headers = { "A", "B", "C", "D" }; Integer[][] data = { oneRow, oneRow, oneRow, oneRow, oneRow, }; DefaultTableModel model = new DefaultTableModel(data, headers); JTable table = new JTable(model); table.setDefaultRenderer(Object.class, new ProgressRenderer(0, maximum)); table.setPreferredScrollableViewportSize(table.getPreferredSize()); frame.add(new JScrollPane(table)); frame.pack();// www .j a v a2 s.c om frame.setVisible(true); new Thread(new Runnable() { @Override public void run() { Object waiter = new Object(); synchronized (waiter) { int rows = model.getRowCount(); int columns = model.getColumnCount(); Random random = new Random(System.currentTimeMillis()); boolean done = false; while (!done) { int row = random.nextInt(rows); int column = random.nextInt(columns); Integer value = (Integer) model.getValueAt(row, column); value++; if (value <= maximum) { model.setValueAt(value, row, column); try { waiter.wait(15); } catch (InterruptedException e) { e.printStackTrace(); } } done = true; for (row = 0; row < rows; row++) { for (column = 0; column < columns; column++) { if (!model.getValueAt(row, column).equals(maximum)) { done = false; break; } } if (!done) { break; } } } } } }).start(); }