How do I generate a list with specified size of random integers within a range in Java 8?

William

I want to generate a list of random numbers between a specified range and of a certain size.

I've tried using streams, and I think this is probably the best solution. I couldn't get it working myself, but I have not learned enough about streams yet.

The following code works for my problem, but I'd like to be able to use something from the Java api if possible. If I were to use stream's map method, I would need to use a consumer with n -> ThreadLocalRandom.current().nextInt(min, max + 1), but I could not appropriately collect at the end of my stream.

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

class Scratch {
    public static void main(String[] args) {
        System.out.println(getRandList(100, 0, 10));
    }

    /**
     * Get n random integers within the range of min and max
     */
    static List<Integer> getRandList(int size, int min, int max) {
        List<Integer> integers = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            integers.add(ThreadLocalRandom.current().nextInt(min, max + 1));
        }
        return integers;
    }
}

The referenced duplicate does not address my question directly, I wanted a List<Integer> and Oleksandr's solution provides a IntStream.

Ravindra Ranwala

How about this,

List<Integer> integers = IntStream.range(0, size)
    .mapToObj(i -> ThreadLocalRandom.current().nextInt(min, max + 1))
    .collect(Collectors.toList());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I generate a list or array of sequential integers in Java?

How can I generate a list or array of sequential integers in Java?

java- how to generate a random hexadecimal value within specified range of values

How can I generate a random number within a range but exclude some?

How to generate list of random integers, but only using specified integers? (Python)

How to generate a list of ascending random integers

How to generate Random coordinates within a circle with specified radius?

How do I generate a vector of random numbers in a range?

Generate N unique random integers within a specified range

How to generate a random number in a list from a range

How do I generate a random derangement of a list/array in java?

How do i generate 2 random numbers, once within the range of 50 and 259, and once within 50 and 159?

How do I generate a random color in a specific range?

Converting random bytes to integers within a specified range

How do I generate a random range of numbers within another range?

Generate random number within specified range without REDUNDANCY in TCL

Need to generate a random integer within a specified range

PHP - Generating random integers within specified range from a key

Using Python how do I generate a random number within a range for each row in Pandas dataframe?

jQuery: how to generate 3 different random integers given limits/range?

How to generate a random number within a range in D

How do I generate a random list in Python with duplicates numbers

NumPy - select random list of unique integers within a range

How to generate a random set of integers from a specified range excluding one integer in matlab

How to generate random sampling in python . with integers. with sum and size given

I want to generate random numbers within given range in java

How do I access integers within an element of a list?

How do I generate random numbers but not repeating and within a certain range based on user input?

How to generate a random number within a range in substrate?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive