Example usage for java.lang Math random

List of usage examples for java.lang Math random

Introduction

In this page you can find the example usage for java.lang Math random.

Prototype

public static double random() 

Source Link

Document

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0 .

Usage

From source file:Main.java

public static void main(String[] args) {
    double number = Math.random();
    System.out.println("Generated number: " + number);

    number = Math.random() * 10;/*from  ww w.j  av a 2 s  . co  m*/
    System.out.println("Generated number: " + number);

    number = 100 + (Math.random() * 100);
    System.out.println("Generated number: " + number);

    int random = 100 + (int) (Math.random() * 100);
    System.out.println("Generated number: " + random);
}

From source file:Mortgage.java

public static void main(String[] args) {
    double payment = Math.random() * 1000;
    System.out.println("Your payment is ");
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    System.out.println(nf.format(payment));
}

From source file:Main.java

public static void main(String[] args) {

    // get two random double numbers
    double x = Math.random();
    double y = Math.random();

    // print the numbers and print the higher one
    System.out.println("Random number 1:" + x);
    System.out.println("Random number 2:" + y);
    System.out.println("Highest number:" + Math.max(x, y));

}

From source file:Main.java

public static void main(String[] args) {
    Supplier<Integer> rndRange = () -> (int) (Math.random() * 100);
    System.out.println(rndRange.get());
}

From source file:Main.java

public static void main(String[] args) {
    int n1 = 1;/*from   w w w  .j  a v  a 2s.com*/
    int n2 = 100;
    double Random;

    Random = n2 + (Math.random() * (n1 - n2));
    System.out.println("Your random number is: " + Random);
}

From source file:Main.java

public static void main(String[] args) {

    for (int i = 0; i < 5; i++) {
        System.out.println("Random Number [" + (i + 1) + "] : " + Math.random());
    }//from  w  w  w  .  ja  va  2s . c  o  m
}

From source file:Main.java

public static void main(String[] args) {
    IntStream i = IntStream.generate(() -> {
        return (int) (Math.random() * 100);
    });//from  www . jav  a  2 s .c o m
    i.limit(10).forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    for (int i = 0; i < 5; i++) {
        System.out.println("Random Number [" + (i + 1) + "] : " + (int) (Math.random() * 100));
    }//ww  w.ja  va2s  . c o  m
}

From source file:Main.java

public static void main(String[] args) {
    int[] list = new int[100];

    for (int i = 0; i < list.length; i++) {
        list[i] = (int) (Math.random() * 50 + 1);
    }//from   www  .j  a va 2s.co m

    int elementInOneLine = 0;

    for (int i = 0; i < list.length; i++) {
        if (elementInOneLine == 10) {
            System.out.println();
            elementInOneLine = 0;
        }
        System.out.print(list[i] + " ");
        elementInOneLine++;
    }
}

From source file:SortArray.java

public static void main(String[] args) {
    int[] a = new int[10];
    for (int i = 0; i < a.length; i++) {
        a[i] = (int) (Math.random() * 100);
    }/*from   www .j a va 2s  . com*/
    Arrays.sort(a);
    System.out.println("Sorted array!");
    for (int i = 0; i < a.length; i++)
        System.out.println(a[i]);
}