Java by API/java.util/Random

Материал из Java эксперт
Перейти к: навигация, поиск

new Random(int intValue)

 
/*
 * Output: 
First generator:
-1193959466
-1139614796
837415749
-1220615319
-1429538713
118249332
-951589224
1301674577
-1638067850
-1279751870

 */
import java.util.Random;
public class MainClass {
  public static void main(String args[]) {
    Random generator = new Random(100);
    System.out.println("First generator:");
    for(int i = 0; i < 10; i++)
      System.out.println(generator.nextInt());
  }
}





Random: nextDouble

 
/*
 * Output:
 * 
 * 0.8102628210601169
 *  
 */
import java.util.Random;
public class MainClass {
  public static void main(String args[]) {
    Random rand = new Random();
    System.out.println(rand.nextDouble());
  }
}





Random: nextGaussian()

 
/*
 * Output:
0.9858761222419792
-1.0868562994825026
-1.2801148369647788
0.03988775024786847
-1.8662098370799078
-0.12277520073012224
0.0911032879532435
-0.0804314384368678
-2.3955991772544114
-1.857516970646183
 */
import java.util.Random;
public class MainClass {
  public static void main(String args[]) {
  
    Random generator = new Random();
    for(int i = 0; i < 10; i++) {
      System.out.println(generator.nextGaussian());
    }
  }
}





Random: nextInt()

 
/*
 * Output: 
1092866141
-1813775291
-538087130
1207492102
19111874
1173765314
-795903398
1623201484
845754190
1812357378
 */
import java.util.Random;
public class MainClass {
  public static void main(String args[]) {
    Random generator = new Random();
    for(int i = 0; i < 10; i++)
      System.out.println(generator.nextInt());
  }
}





Random: nextLong()

      
import java.util.Random;
public class Main {
  public static void main(String[] args) {
    Random rand = new Random();
    int randomInt = rand.nextInt(10);
    long randomLong = rand.nextLong() * 10;
    float randomFloat = rand.nextLong() * 10;
    double randomDouble = rand.nextDouble() * 10;
  }
}