Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id C1EDF200CDB for ; Sat, 5 Aug 2017 20:37:54 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id AEEBB164FE0; Sat, 5 Aug 2017 18:37:54 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id F0336164FDF for ; Sat, 5 Aug 2017 20:37:53 +0200 (CEST) Received: (qmail 28846 invoked by uid 500); 5 Aug 2017 18:37:48 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 28834 invoked by uid 99); 5 Aug 2017 18:37:47 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 05 Aug 2017 18:37:47 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id E31EAC023D for ; Sat, 5 Aug 2017 18:37:46 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -0.72 X-Spam-Level: X-Spam-Status: No, score=-0.72 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01] autolearn=disabled Authentication-Results: spamd4-us-west.apache.org (amavisd-new); dkim=pass (1024-bit key) header.d=scarlet.be Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id e9COCkQs6ti3 for ; Sat, 5 Aug 2017 18:37:45 +0000 (UTC) Received: from hel.is.scarlet.be (hel.is.scarlet.be [193.74.71.26]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTPS id 855ED5F6C7 for ; Sat, 5 Aug 2017 18:37:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=scarlet.be; s=scarlet; t=1501958257; bh=lkddsTj+0AITGN1GvYZ31PgQuECoNOFjPVuiH4E9CgM=; h=MIME-Version:Content-Type:Content-Transfer-Encoding:Date:From:To: Subject:In-Reply-To:References:Message-ID; b=koXCsMKuNhHLwyML8oKG7IReOyPfEtLgv740EndYWrKG4jBgNSpy8lPzsp0UOwIs2 UtWkn20t6MG34IuEu2GoIO+sIhW2Y1Sc6DtrRJZOsPY/t7v1n5AwXi2EfSeSJzP49d RLrR2aGHHXiCSOUhape6qVLcRa7Hiilwq+lYqWwE= Received: from webmail.scarlet.be (gresham.is.scarlet.be [193.74.71.215]) by hel.is.scarlet.be (8.14.9/8.14.9) with ESMTP id v75IbalC019680 for ; Sat, 5 Aug 2017 20:37:37 +0200 X-Scarlet: d=1501958257 c=193.74.71.215 Received: from ip-213-49-207-217.dsl.scarlet.be ([213.49.207.217]) via ip-213-49-207-217.dsl.scarlet.be ([213.49.207.217]) by webmail.scarlet.be with HTTP (HTTP/1.1 POST); Sat, 05 Aug 2017 20:37:36 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Date: Sat, 05 Aug 2017 20:37:36 +0200 From: Gilles To: Subject: Re: [RNG] Generating for enums In-Reply-To: References: <16f1acdf5bc7aa0333d9e5caa0835a63@scarlet.be> Message-ID: X-Sender: gilles@harfang.homelinux.org User-Agent: Scarlet Webmail X-DCC-scarlet.be-Metrics: hel; whitelist X-Virus-Scanned: clamav-milter 0.98.1-exp at hel X-Virus-Status: Clean archived-at: Sat, 05 Aug 2017 18:37:55 -0000 On Sat, 5 Aug 2017 09:47:20 -0700, Gary Gregory wrote: > On Fri, Aug 4, 2017 at 1:59 PM, Gilles > wrote: > >> Hi Gary. >> >> On Fri, 4 Aug 2017 11:10:57 -0700, Gary Gregory wrote: >> >>> For example, I have a enum like: >>> >>> public enum CardinalDirection (NORTH,SOUTH,EAST,WEST) >>> >>> and I want to say >>> >>> traveler.travel(nextRandomDirection()); >>> >>> where >>> >>> public CardinalDirection nextRandomDirection() { >>> return rng.next(CardinalDirection.class); >>> } >>> >> >> Actually, "Commons RNG" already provides all the necessary >> functionality >> for a one-liner: >> >> ---CUT--- >> import java.util.Arrays; >> import org.apache.commons.rng.simple.RandomSource; >> import org.apache.commons.rng.sampling.CollectionSampler; >> >> CollectionSampler r >> = new >> CollectionSampler<>(RandomSource.create(RandomSource.SPLIT_ >> MIX_64), >> >> Arrays.asList(CardinalDirection.values())); >> >> CardinalDirection e = r.sample(); >> ---CUT--- >> > > Very nice, thank you. > > I can see a wrapper for this boilerplate though: > > public class RandomEnum { > > private final Class enumClass; > private final CollectionSampler rng; > > public RandomEnum(final Class enumClass) { > super(); > this.enumClass = enumClass; > this.rng = new > CollectionSampler<>(RandomSource.create(RandomSource.SPLIT_MIX_64), > Arrays.asList(CellDirection.values())); > > } > > public CellDirection next() { > return rng.sample(); > } > > @Override > public String toString() { > return "RandomEnum [rng=" + rng + ", enumClass=" + enumClass + "]"; > } > } I guess there is a mix of generics and "specifics" here (cf. "CellDirection"). And if you have only have a generic "E" in that class, you won't be able to call "E.values()". Then see also my other post: http://markmail.org/message/h63yb7ozk4nyjagj However, what does one gain with having to call this one-liner: RandomEnum r = new RandomEnum(SomeEnum.class, RandomSource.create(RandomSource.SPLIT_MIX_64)); vs the one I've suggested above? Gilles >> [...] --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org For additional commands, e-mail: dev-help@commons.apache.org