Path: csiph.com!news.mixmin.net!news.albasani.net!.POSTED!not-for-mail From: Sebastian Newsgroups: comp.lang.java.programmer Subject: Random permutations [was Re: Libraries for sysadmins?] Date: Sat, 05 Mar 2016 00:56:51 +0100 Organization: albasani.net Lines: 33 Message-ID: References: <2cec6af2-9636-4a2a-8849-b01286c0814c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.albasani.net C+r95DPoqXobaOGZSn+cwVUXK4x+/yqqNmgwnRYA25ciwHA+WHKN3EXKQvkzTUZ4R6KD8EZpP3g2CJJ0OL3kqQ== NNTP-Posting-Date: Fri, 4 Mar 2016 23:56:51 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="bbIduCK18uRbbRPlQt3bp+Wui364RbwAZxEsAm78xPtS4z2MWjV1l79MmHpFaQPNyD3sp+UCWycsQzB5Wbq7mssAOFmmI6tlMaxRD8c6Raad6jo7pfXrrPPYehTjoM6v"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 In-Reply-To: Cancel-Lock: sha1:AuS5caJS3gE4JZrbwqtcpc2RnI0= Xref: csiph.com comp.lang.java.programmer:33925 Am 04.03.2016 21:13, schrieb Stefan Ram: [snip] > > I like to investigate how to concisely write a program to print > numbers simulating the German lotto »6 from 49«, where six > numbers are chosen from 1..49 in different programming languages. > > Here is my Java version: > > public class Main > { public static void main( final java.lang.String[] args ) > { final java.util.List numbers = > java.util.stream.IntStream.rangeClosed( 1, 49 ).boxed(). > collect( java.util.stream.Collectors.toList() ); > java.util.Collections.shuffle( numbers ); > java.lang.System.out.println( numbers.subList( 0, 6 ) ); }} > [snip] This is good if you need about as many elements in the result as are in the range. But if the range is large and you need only a few elements, it may be better to generate numbers (without duplicates), like this: new Random().ints(1, 50).distinct().limit(6) [Side note, not relevant to the lotto example: IntStream#distinct() uses a LinkedHashSet internally, so that the order is maintained, as required by the java docs for Stream#distinct().] -- Sebastian PS: Whoever taught you to qualify the name of classes from java.lang?