Java Code Examples for java.util.Random#ints()

The following examples show how to use java.util.Random#ints() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Java8RandomExample.java    From chuidiang-ejemplos with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) {
    Random random = new Random();

    // IntStream for values between 1 and 6 both included
    final IntStream ints = random.ints(1, 7);

    // Iterator to get integers
    final PrimitiveIterator.OfInt iterator = ints.iterator();

    // Loop to print 10 integers.
    int counter=0;
    while (iterator.hasNext() && counter<10){
        System.out.println(iterator.next());
        counter++;
    }

    // Close de stream
    ints.close();
}
 
Example 2
Source File: Java8RandomExample.java    From chuidiang-ejemplos with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {
    Random random = new Random();

    IntStream intStream = random.ints(10, 1, 7);
    Iterator iterator = intStream.iterator();

    while (iterator.hasNext()){
        System.out.println("Random Number "+iterator.next());
    }

    intStream = random.ints(10, 1, 7);
    intStream.forEach(value ->
            System.out.println("Random Number "+value)
    );
}
 
Example 3
Source File: RandomNumbersGenerator.java    From tutorials with MIT License 4 votes vote down vote up
public IntStream generateRandomUnlimitedIntStream() {
    Random random = new Random();
    IntStream unlimitedIntStream = random.ints();
    return unlimitedIntStream;
}
 
Example 4
Source File: RandomNumbersGenerator.java    From tutorials with MIT License 4 votes vote down vote up
public IntStream generateRandomLimitedIntStream(long streamSize) {
    Random random = new Random();
    IntStream limitedIntStream = random.ints(streamSize);
    return limitedIntStream;
}
 
Example 5
Source File: RandomNumbersGenerator.java    From tutorials with MIT License 4 votes vote down vote up
public IntStream generateRandomLimitedIntStreamWithinARange(int min, int max, long streamSize) {
    Random random = new Random();
    IntStream limitedIntStreamWithinARange = random.ints(streamSize, min, max);
    return limitedIntStreamWithinARange;
}