List of usage examples for java.lang Math random
public static double random()
From source file:RandNum.java
public static void main(String[] args) { int num;// w w w . j a v a 2 s. c o m int[] dist = new int[10]; // Storage for distribution // Generate 10000 random numbers using Math.random() for (int x = 0; x < 10000; x++) { num = (int) (Math.floor(Math.random() * 10)); dist[num]++; } // Display distribution of random integers in the range // 0 to 9 System.out.println("Distribution using Math.random() "); for (int k = 0; k < 10; k++) System.out.print(k + "\t"); // Display results for (int y = 0; y < 10; y++) { System.out.print(dist[y] + "\t"); } System.out.println(); }
From source file:AnimationTester.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ArrayComponent panel = new ArrayComponent(); frame.add(panel, BorderLayout.CENTER); frame.setSize(800, 300);//w w w .j ava 2 s. co m frame.setVisible(true); Double[] values = new Double[100]; for (int i = 0; i < values.length; i++) values[i] = Math.random() * panel.getHeight(); final Sorter sorter = new Sorter(values, panel); Thread sorterThread = new Thread(sorter); sorterThread.start(); }
From source file:Main.java
public static void main(String[] args) { int a = 10;//from ww w . ja v a 2 s. c om int b = -50; int c = 3; double x = 25.0; double y = 3.0; double z = 4.0; System.out.println("abs(b) = " + Math.abs(b)); System.out.println("cbrt(x) = " + Math.cbrt(x)); System.out.println("exp(y) = " + Math.exp(z)); System.out.println("hypot(y, z)= " + Math.hypot(y, z)); System.out.println("log(y) = " + Math.log(y)); System.out.println("log10(y) = " + Math.log10(y)); System.out.println("max(a, b) = " + Math.max(a, b)); System.out.println("min(a, b) = " + Math.min(a, b)); System.out.println("pow(a, c) = " + Math.pow(a, c)); System.out.println("random() = " + Math.random()); System.out.println("signum(b) = " + Math.signum(b)); System.out.println("sqrt(x) = " + Math.sqrt(y)); }
From source file:mase.deprecated.FastMathTest.java
public static void main(String[] args) { double MIN = -10; double MAX = 10; int N = 10000; DescriptiveStatistics diff = new DescriptiveStatistics(); DescriptiveStatistics diffJava = new DescriptiveStatistics(); long tFast = 0, tNormal = 0, tBounded = 0, tJava = 0; for (int i = 0; i < N; i++) { double x = Math.random() * (MAX - MIN) + MIN; long t = System.nanoTime(); double v1 = (1.0 / (1.0 + FastMath.expQuick(-1 * x))); tFast += System.nanoTime() - t; t = System.nanoTime();/* ww w . ja va2s. com*/ double v2 = (1.0 / (1.0 + FastMath.exp(-1 * x))); tNormal += System.nanoTime() - t; t = System.nanoTime(); double v3 = (1.0 / (1.0 + BoundMath.exp(-1 * x))); tBounded += System.nanoTime() - t; t = System.nanoTime(); double v4 = (1.0 / (1.0 + Math.exp(-1 * x))); tJava += System.nanoTime() - t; diff.addValue(Math.abs(v1 - v2)); diffJava.addValue(Math.abs(v3 - v1)); } System.out.println("MAX: " + diff.getMax()); System.out.println("MEAN: " + diff.getMean()); System.out.println("MAX JAVA: " + diffJava.getMax()); System.out.println("MEAN JAVA: " + diffJava.getMean()); System.out.println("Fast: " + tFast); System.out.println("Normal: " + tNormal); System.out.println("Bounded: " + tBounded); System.out.println("Java: " + tJava); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { File aFile = new File("C:/test.bin"); RandomAccessFile ioFile = new RandomAccessFile(aFile, " rw"); FileChannel ioChannel = ioFile.getChannel(); final int PRIMESREQUIRED = 10; long[] primes = new long[PRIMESREQUIRED]; int index = 0; final long REPLACEMENT = 999999L; final int PRIMECOUNT = (int) ioChannel.size() / 8; MappedByteBuffer buf = ioChannel.map(FileChannel.MapMode.READ_WRITE, 0L, ioChannel.size()).load(); ioChannel.close();/*from w w w. j av a 2 s. com*/ for (int i = 0; i < PRIMESREQUIRED; i++) { index = 8 * (int) (PRIMECOUNT * Math.random()); primes[i] = buf.getLong(index); buf.putLong(index, REPLACEMENT); } for (long prime : primes) { System.out.printf("%12d", prime); } ioFile.close(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { createFile();/*from www . j ava 2 s .c om*/ File aFile = new File("C:/primes.bin"); FileInputStream inFile = new FileInputStream(aFile); FileChannel inChannel = inFile.getChannel(); final int PRIMESREQUIRED = 10; ByteBuffer buf = ByteBuffer.allocate(8 * PRIMESREQUIRED); long[] primes = new long[PRIMESREQUIRED]; int index = 0; // Position for a prime in the file // Count of primes in the file final int PRIMECOUNT = (int) inChannel.size() / 8; // Read the number of random primes required for (int i = 0; i < PRIMESREQUIRED; i++) { index = 8 * (int) (PRIMECOUNT * Math.random()); inChannel.read(buf, index); // Read the value buf.flip(); primes[i] = buf.getLong(); // Save it in the array buf.clear(); } for (long prime : primes) { System.out.printf("%12d", prime); } inFile.close(); // Close the file and the channel }
From source file:Wake.java
public static void main(String args[]) { Wake queue = new Wake(); Runnable r = new RunThis(queue); Thread t;//from w ww . j ava 2s . co m for (int i = 0; i < 10; i++) { t = new Thread(r); t.start(); } for (int i = 0; i < 11; i++) { try { Thread.sleep((long) (Math.random() * 1000)); } catch (InterruptedException e) { } System.out.println("About to wake one thread"); queue.wakeOne(); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Document document = new Document(PageSize.A4.rotate()); PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); document.open();/*from ww w . j av a 2 s .c o m*/ PdfPTable datatable = new PdfPTable(10); int headerwidths[] = { 10, 24, 12, 12, 7, 7, 7, 7, 7, 7 }; datatable.setWidths(headerwidths); datatable.setWidthPercentage(100); datatable.getDefaultCell().setPadding(5); datatable.setHeaderRows(2); datatable.getDefaultCell().setBorderWidth(1); for (int i = 1; i < 30; i++) { datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); datatable.addCell("myUserId"); datatable.addCell("long long name"); datatable.addCell("No Name Company"); datatable.addCell("D" + i); datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); for (int j = 0; j < 6; j++) datatable.addCell(Math.random() > .5 ? "Yes" : "No"); } datatable.setSplitLate(false); document.add(datatable); document.close(); }
From source file:SharedDataSample.java
public static void main(String args[]) { final String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }; final DefaultComboBoxModel model = new DefaultComboBoxModel(labels); JFrame frame = new JFrame("Shared Data"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); JPanel panel = new JPanel(); JComboBox comboBox1 = new JComboBox(model); comboBox1.setMaximumRowCount(5);// w w w . j a v a2 s.co m comboBox1.setEditable(true); JComboBox comboBox2 = new JComboBox(model); comboBox2.setMaximumRowCount(5); comboBox2.setEditable(true); panel.add(comboBox1); panel.add(comboBox2); contentPane.add(panel, BorderLayout.NORTH); JList jlist = new JList(model); JScrollPane scrollPane = new JScrollPane(jlist); contentPane.add(scrollPane, BorderLayout.CENTER); JButton button = new JButton("Add"); contentPane.add(button, BorderLayout.SOUTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int index = (int) (Math.random() * labels.length); model.addElement(labels[index]); } }; button.addActionListener(actionListener); frame.setSize(300, 200); frame.setVisible(true); }
From source file:LotteryDrawing.java
public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("How many numbers do you need to draw? "); int k = in.nextInt(); System.out.print("What is the highest number you can draw? "); int n = in.nextInt(); // fill an array with numbers 1 2 3 . . . n int[] numbers = new int[n]; for (int i = 0; i < numbers.length; i++) numbers[i] = i + 1;//from w ww. j a va 2 s. com // draw k numbers and put them into a second array int[] result = new int[k]; for (int i = 0; i < result.length; i++) { // make a random index between 0 and n - 1 int r = (int) (Math.random() * n); // pick the element at the random location result[i] = numbers[r]; // move the last element into the random location numbers[r] = numbers[n - 1]; n--; } // print the sorted array Arrays.sort(result); System.out.println("Bet the following combination. It'll make you rich!"); for (int r : result) System.out.println(r); }