Synthia
Generic and flexible data structure generator
RandomPicker.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2021 Laboratoire d'informatique formelle
4  Université du Québec à Chicoutimi, Canada
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Lesser General Public License for more details.
13  You should have received a copy of the GNU Lesser General Public License
14  along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 package ca.uqac.lif.synthia.random;
17 
18 import ca.uqac.lif.petitpoucet.NodeFactory;
19 import ca.uqac.lif.petitpoucet.Part;
20 import ca.uqac.lif.petitpoucet.PartNode;
21 import ca.uqac.lif.petitpoucet.function.ExplanationQueryable;
22 import ca.uqac.lif.synthia.Picker;
23 import ca.uqac.lif.synthia.Seedable;
24 
25 /**
26  * Picks an object based on the value of a random number generator. All
27  * descendants of this class use an instance of Java's {@link Random}
28  * class. They also implement the {@link Seedable} interface, which means
29  * that this internal generator can be seeded with some value; furthermore,
30  * a call to {@link #reset()} will instantiate a new instance of
31  * <tt>Random</tt> with the initial seed. This means that resetting a
32  * <tt>RandomPicker</tt> should in principle make it output the same sequence
33  * of values as when it was first created.
34  *
35  * @param <T> The type of object to pick
36  *
37  * @ingroup API
38  */
39 public abstract class RandomPicker<T> implements Picker<T>, Seedable, ExplanationQueryable
40 {
41  /**
42  * A random number generator used to set the seed when none is
43  * specified
44  */
45  protected static final transient Random s_random = new Random();
46 
47  /*@ non_null @*/ protected transient Random m_random;
48 
49  protected int m_seed;
50 
51  public RandomPicker(int seed)
52  {
53  super();
54  setSeed(seed);
55  }
56 
57  public RandomPicker() { this(s_random.nextInt()); }
58 
59  @Override
60  public RandomPicker<T> setSeed(int seed)
61  {
62  m_seed = seed;
63  m_random = new Random(seed);
64  return this;
65  }
66 
67  @Override
68  public void reset()
69  {
70  m_random = new Random(m_seed);
71  }
72 
73  @Override
74  public PartNode getExplanation(Part p)
75  {
76  return getExplanation(p, NodeFactory.getFactory());
77  }
78 
79  @Override
80  public PartNode getExplanation(Part p, NodeFactory f)
81  {
82  return f.getPartNode(p, this);
83  }
84 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.random.RandomPicker.getExplanation
PartNode getExplanation(Part p)
Definition: RandomPicker.java:74
ca.uqac
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif
ca.uqac.lif.synthia.random.RandomPicker.s_random
static final transient Random s_random
A random number generator used to set the seed when none is specified.
Definition: RandomPicker.java:45
ca.uqac.lif.synthia.random.Random.nextInt
int nextInt()
Returns the next pseudorandom, uniformly distributed.
Definition: Random.java:348
ca.uqac.lif.synthia.random.RandomPicker.reset
void reset()
Puts the picker back into its initial state.
Definition: RandomPicker.java:68
ca.uqac.lif.synthia.random.RandomPicker.RandomPicker
RandomPicker()
Definition: RandomPicker.java:57
ca
ca.uqac.lif.synthia.random.RandomPicker
Picks an object based on the value of a random number generator.
Definition: RandomPicker.java:39
ca.uqac.lif.synthia.random.Random
An instance of this class is used to generate a stream of pseudorandom numbers.
Definition: Random.java:75
ca.uqac.lif.synthia.random.RandomPicker.getExplanation
PartNode getExplanation(Part p, NodeFactory f)
Definition: RandomPicker.java:80
ca.uqac.lif.synthia.random.RandomPicker.setSeed
RandomPicker< T > setSeed(int seed)
Set the seed of the random generator.
Definition: RandomPicker.java:60
ca.uqac.lif.synthia.random.RandomPicker.m_seed
int m_seed
Definition: RandomPicker.java:49
ca.uqac.lif.synthia.Seedable
Interface implemented by objects that can be seeded.
Definition: Seedable.java:29
ca.uqac.lif.synthia.random.RandomPicker.m_random
transient Random m_random
Definition: RandomPicker.java:47
ca.uqac.lif.synthia.random.RandomPicker.RandomPicker
RandomPicker(int seed)
Definition: RandomPicker.java:51