<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
While I'm sympathetic to the use-case, I think adding such a method to List is the wrong moveˇXyou could equally argue that there should be a randomIterator(), a randomSet(), a randomAdd(). And it wouldn't be coherent with the specification of the List-methods
 to create a wrapper-type which would perform the randomization (i.e. that set(i, e) would add it at a random index rather than at i).<br>
It's also important to note that we cannot presume to know what level of randomness is required by the developer, so a RandomGenerator would need to be provided.</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
One could, however, argue whether there should be a convenience-method on RandomGenerator named something akin to "<T, L extends List<? extends T> & RandomAccess> T nextElement(L list)" however the discoverability of said method would be less-than-ideal.</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
This leads me to conclude that it is arguably better as a static utility method in the developer's codebase, or inlining it at use-site. After all, it's half a line of code, presuming of course that the author has already checked for emptiness:<br>
<br>
var e = list.get(rand.nextInt(list.size());<br>
<br>
vs<br>
<br>
</div>
<div style="line-height: 18px; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
public static <T, L extends List<? extends T> & RandomAccess> T randomElement(RandomGenerator rand, L list) {</div>
<div style="line-height: 18px; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
    if(list.isEmpty()) // Implicit null-check of list</div>
<div style="line-height: 18px; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
        throw new NoSuchElementException();</div>
<div style="line-height: 18px; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
    else</div>
<div style="line-height: 18px; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
        return list.get(rand.nextInt(list.size())); // Implicit null-check of rand</div>
<div style="line-height: 18px; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
}<br>
<br>
var e = randomElement(rand, list);</div>
<div id="Signature">
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Cheers,<br>
ˇÔ</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<b><br>
</b></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<b>Viktor Klang</b></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Software Architect, Java Platform Group<br>
Oracle</div>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> core-libs-dev <core-libs-dev-retn@openjdk.org> on behalf of Alan Bateman <alan.bateman@oracle.com><br>
<b>Sent:</b> Sunday, 24 August 2025 11:42<br>
<b>To:</b> Daniel Tavares <cetr1n@gmail.com>; core-libs-dev@openjdk.org <core-libs-dev@openjdk.org><br>
<b>Subject:</b> Re: Proposal: Add getRandom() default method to java.util.List</font>
<div> </div>
</div>
<div>
<div class="x_moz-cite-prefix">On 23/08/2025 20:36, Daniel Tavares wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">:
<p><b>Proposed Method</b></p>
<p>default T getRandom() {<br>
    if (isEmpty()) return null;<br>
    int index = ThreadLocalRandom.current().nextInt(size());<br>
    return get(index);<br>
}</p>
</div>
</blockquote>
Add Lists that are mutable and support concurrent access to your list to think about. These implementations would need to be updated to implement this method, otherwise you risk getting IOOBE with the default method.<br>
<br>
-Alan<br>
</div>
</body>
</html>