current position:Home>How does Java randomly get elements from a list

How does Java randomly get elements from a list

2022-04-29 04:34:4651CTO

summary

From a List Getting an element at random is about List A basic operation of , But this operation is not very obvious .

This page mainly shows you how to effectively start from List Get a random element and some methods you can use .

Select a random element

In order to List Get an element at random , You can randomly choose from List Get the subscript of an index in .

Then you can use this random subscript method  List.get()  To get elements at random .

The point of using this method is , Randomly generated subscripts should not exceed  List’s  Size , Otherwise, you will encounter overflow exceptions .

Single random element

To get a random subscript , You can use  Random.nextInt(int bound)  Method .

Look at the following code :

      
      
public void givenList_shouldReturnARandomElement() {
List<Integer> givenList = Arrays.asList(1, 2, 3);
Random rand = new Random();
int randomElement = givenList.get(rand.nextInt(givenList.size()));
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

If you don't want to use it  Random  class , You can use  Math.random()  This static method , Then multiply the generated random number by List Size . (Math.random()) Will generate  Double  Random variable of .

The interval of this random variable is 0 and 1 Between , This variable contains 0, But it doesn't include 1.


Java How from a List Get elements randomly from _ random number


After completing the above conversion , Don't forget to convert the obtained subscript to int type .

Select random subscripts in a multithreaded environment

In a multithreaded environment , How to use a single  Random  Class instance , This will cause every thread in this instance to access the same result .

We can use  ThreadLocalRandom  Class to create a new instance for each thread .

For example, the following code can ensure that the same random subscripts do not appear in multiple threads .

      
      
int randomElementIndex
= ThreadLocalRandom.current().nextInt(listSize) % givenList.size();
  • 1.
  • 2.

You can repeatedly select elements

occasionally , You may want to start from List Select some elements in , But these selected elements can be repeated .

The operation required is also very simple , Choose as many as you need , Just use a loop to do it .

Like the following code , Direct use cycle .

      
      
public void givenList_whenNumberElementsChosen_shouldReturnRandomElementsRepeat() {
Random rand = new Random();
List<String> givenList = Arrays.asList("one", "two", "three", "four");

int numberOfElements = 2;

for (int i = 0; i < numberOfElements; i++) {
int randomIndex = rand.nextInt(givenList.size());
String randomElement = givenList.get(randomIndex);
}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

Select non repeating elements

If you want the elements you choose not to repeat , You can change the selected elements from List Remove .

It should be noted that we are worried about the overflow of subscripts .

Like yours List The length is 10, But you want from here List Choose from 11 A non repeating element , There is no way to do this .

Usually throw an exception .

      
      
public void givenList_whenNumberElementsChosen_shouldReturnRandomElementsNoRepeat() {
Random rand = new Random();
List<String> givenList = Lists.newArrayList("one", "two", "three", "four");

int numberOfElements = 2;

for (int i = 0; i < numberOfElements; i++) {
int randomIndex = rand.nextInt(givenList.size());
String randomElement = givenList.get(randomIndex);
givenList.remove(randomIndex);
}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

Choose a random sequence

The main purpose of this method is to give List Make a mess of , And get the same random sequence .

Of course, you can also use loops to do , If you use  Collections  Tools can better help you achieve this goal :

      
      
public void givenList_whenSeriesLengthChosen_shouldReturnRandomSeries() {
List<Integer> givenList = Lists.newArrayList(1, 2, 3, 4, 5, 6);
Collections.shuffle(givenList);

int randomSeriesLength = 3;

List<Integer> randomSeries = givenList.subList(0, randomSeriesLength);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

Conclusion

In this page , We are right. List The random output of elements in .

Through the above , Can better help you understand about List Random output in .


 ​https://www.ossez.com/t/java-list/13934​

copyright notice
author[51CTO],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/04/202204290434413917.html

Random recommended