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

How does Java randomly get elements from a list

2022-04-29 07:08:40InfoQ

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()));
}
If you don't want to use it &nbsp;
Random
&nbsp; class , You can use &nbsp;
Math.random()
&nbsp; This static method , Then multiply the generated random number by  List  Size . (
Math.random()
) Will generate &nbsp;
Double
&nbsp; Random variable of .
The interval of this random variable is  0  and  1  Between , This variable contains  0, But it doesn't include  1.
&nbsp;

null

&nbsp;
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 &nbsp;
Random
&nbsp; Class instance , This will cause every thread in this instance to access the same result .
We can use &nbsp;
ThreadLocalRandom
&nbsp; 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();


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(&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;);

 int numberOfElements = 2;

 for (int i = 0; i < numberOfElements; i++) {
 int randomIndex = rand.nextInt(givenList.size());
 String randomElement = givenList.get(randomIndex);
 }
}


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(&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;);

 int numberOfElements = 2;

 for (int i = 0; i < numberOfElements; i++) {
 int randomIndex = rand.nextInt(givenList.size());
 String randomElement = givenList.get(randomIndex);
 givenList.remove(randomIndex);
 }
}


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 &nbsp;
Collections
&nbsp; 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);
}


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 .
&nbsp;
https://www.ossez.com/t/java-list/13934

copyright notice
author[InfoQ],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204290403428885.html

Random recommended