public static void main(String[] args) { Random rand = new Random(47); Integer[] a = new Integer[rand.nextInt(20)]; print("length of a = " + a.length); for (int i = 0; i < a.length; i++) a[i] = rand.nextInt(500); // Autoboxing print(Arrays.toString(a)); }
public static void main(String[] args) { Random rand = new Random(); int value, value2; value2 = rand.nextInt(100); for (int i = 0; i < 25; i++) { if ((value = rand.nextInt(100)) > value2) print(value + " is larger than " + value2); else print(value + " is less than " + value2); } }
// Insert a random delay to produce the effect // of a calculation time: private void pause(int factor) { try { TimeUnit.MILLISECONDS.sleep(100 + rand.nextInt(factor)); } catch (InterruptedException e) { print("sleep() interrupted"); } }
public static void main(String[] args) { Random rand = new Random(47); int i = rand.nextInt(); int j = rand.nextInt(); printBinaryInt("-1", -1); printBinaryInt("+1", +1); int maxpos = 2147483647; printBinaryInt("maxpos", maxpos); int maxneg = -2147483648; printBinaryInt("maxneg", maxneg); printBinaryInt("i", i); printBinaryInt("~i", ~i); printBinaryInt("-i", -i); printBinaryInt("j", j); printBinaryInt("i & j", i & j); printBinaryInt("i | j", i | j); printBinaryInt("i ^ j", i ^ j); printBinaryInt("i << 5", i << 5); printBinaryInt("i >> 5", i >> 5); printBinaryInt("(~i) >> 5", (~i) >> 5); printBinaryInt("i >>> 5", i >>> 5); printBinaryInt("(~i) >>> 5", (~i) >>> 5); long l = rand.nextLong(); long m = rand.nextLong(); printBinaryLong("-1L", -1L); printBinaryLong("+1L", +1L); long ll = 9223372036854775807L; printBinaryLong("maxpos", ll); long lln = -9223372036854775808L; printBinaryLong("maxneg", lln); printBinaryLong("l", l); printBinaryLong("~l", ~l); printBinaryLong("-l", -l); printBinaryLong("m", m); printBinaryLong("l & m", l & m); printBinaryLong("l | m", l | m); printBinaryLong("l ^ m", l ^ m); printBinaryLong("l << 5", l << 5); printBinaryLong("l >> 5", l >> 5); printBinaryLong("(~l) >> 5", (~l) >> 5); printBinaryLong("l >>> 5", l >>> 5); printBinaryLong("(~l) >>> 5", (~l) >>> 5); }
public static void main(String[] args) { Random rand = new Random(47); for (int i = 0; i < 100; i++) { int c = rand.nextInt(26) + 'a'; printnb((char) c + ", " + c + ": "); switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': print("vowel"); break; case 'y': case 'w': print("Sometimes a vowel"); break; default: print("consonant"); } } }
public void run() { try { while (!Thread.interrupted()) { TimeUnit.MILLISECONDS.sleep(100 + rand.nextInt(500)); // Make toast Toast t = new Toast(count++); print(t); // Insert into queue toastQueue.put(t); } } catch (InterruptedException e) { print("Toaster interrupted"); } print("Toaster off"); }
private void pause() throws InterruptedException { if (ponderFactor == 0) return; TimeUnit.MILLISECONDS.sleep(rand.nextInt(ponderFactor * 250)); }