Synthia
Generic and flexible data structure generator
RandomInteger.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2020 Laboratoire d'informatique formelle
4  Université du Québec à Chicoutimi, Canada
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 package ca.uqac.lif.synthia.random;
20 
21 import ca.uqac.lif.synthia.Picker;
22 import ca.uqac.lif.synthia.Reactive;
25 
26 /**
27  * Picks an integer uniformly in an interval.
28  *
29  * @ingroup API
30  */
31 public class RandomInteger extends RandomPicker<Integer> implements Shrinkable<Integer>, Reactive<Integer,Integer>
32 {
33  /**
34  * The lower bound of the interval
35  */
36  protected int m_min;
37 
38  /**
39  * The higher bound of the interval
40  */
41  protected int m_max;
42 
43  /**
44  * Default constructor
45  */
46  public RandomInteger()
47  {
48  super();
49  setInterval(0,1);
50  }
51 
52 
53  /**
54  * Creates a new instance of the picker.
55  * @param min The lower bound of the interval
56  * @param max The higher bound of the interval
57  */
58  public RandomInteger(int min, int max)
59  {
60  super();
61  setInterval(min, max);
62  }
63 
64  /**
65  * Protected constructor used to duplicate the picker.
66  * @param min The lower bound of the interval.
67  * @param max The higher bound of the interval.
68  * @param seed The initial seed of the random genarator.
69  * @param random The random generator.
70  */
71  protected RandomInteger(int min, int max, int seed, Random random)
72  {
73  m_min = min;
74  m_max = max;
75  m_seed = seed;
76  m_random = random;
77  }
78 
79  /**
80  * Sets the interval in which integers are picked
81  * @param min The lower bound of the interval
82  * @param max The higher bound of the interval
83  */
84  public RandomInteger setInterval(int min, int max)
85  {
86  m_min = min;
87  m_max = max;
88  return this;
89  }
90 
91  /**
92  * Sets the maximum bound for values of this picker.
93  * @param max The higher bound of the interval
94  */
95  @Override
96  public void tell(Integer max)
97  {
98  m_max = max;
99  }
100 
101  @Override
102  public RandomInteger setSeed(int seed)
103  {
104  super.setSeed(seed);
105  return this;
106  }
107 
108 
109  /**
110  * Picks a random integer. Typically, this method is expected to return non-null
111  * objects; a <tt>null</tt> return value is used to signal that no more
112  * objects will be produced. That is, once this method returns
113  * <tt>null</tt>, it should normally return <tt>null</tt> on all subsequent
114  * calls.
115  * @return The random integer
116  */
117  @Override
118  public Integer pick()
119  {
120  return m_random.nextInt(m_max - m_min) + m_min;
121  }
122 
123 
124  /**
125  * Creates a copy of the RandomInteger picker.
126  * @param with_state If set to <tt>false</tt>, the returned copy is set to
127  * the class' initial state (i.e. same thing as calling the picker's
128  * constructor). If set to <tt>true</tt>, the returned copy is put into the
129  * same internal state as the object it is copied from.
130  * @return The copy of the RandomInteger picker
131  */
132  @Override
133  public RandomInteger duplicate(boolean with_state)
134  {
136  if (!with_state)
137  {
138  copy.reset();
139  }
140  return copy;
141  }
142 
143  @Override
144  public Shrinkable<Integer> shrink(Integer element, Picker<Float> decision, float magnitude)
145  {
146  if(element <= m_min)
147  {
148  return new NothingPicker<Integer>();
149  }
150  else
151  {
152 
153  int new_bound = (int) (element * magnitude);
154  if (new_bound <= m_min)
155  {
156  new_bound = m_min + 1;
157  }
158  return new RandomInteger(m_min, new_bound, m_seed, m_random.Duplicate());
159  }
160  }
161 
162  @Override
163  public String toString()
164  {
165  return "RandomInteger [" + m_min + "," + m_max + "]";
166  }
167 
168 
169  @Override
170  public Shrinkable<Integer> shrink(Integer o)
171  {
172  return shrink(o, RandomFloat.instance, 1);
173  }
174 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.random.RandomInteger.m_min
int m_min
The lower bound of the interval.
Definition: RandomInteger.java:36
ca.uqac.lif.synthia.Shrinkable
Interface signaling that a picker can be shrunk.
Definition: Shrinkable.java:36
ca.uqac.lif.synthia.Reactive
Interface implemented by pickers whose picking of objects can be altered by external information.
Definition: Reactive.java:43
ca.uqac.lif.synthia.random.RandomInteger.shrink
Shrinkable< Integer > shrink(Integer o)
Definition: RandomInteger.java:170
ca.uqac.lif.synthia.util
Miscellaneous pickers performing various functions.
Definition: ArrayPicker.java:19
ca.uqac.lif.synthia.random.RandomFloat.instance
static final transient RandomFloat instance
A public static instance of RandomFloat.
Definition: RandomFloat.java:45
ca.uqac.lif.synthia.random.RandomInteger.RandomInteger
RandomInteger(int min, int max)
Creates a new instance of the picker.
Definition: RandomInteger.java:58
ca.uqac
ca.uqac.lif.synthia.random.RandomInteger.RandomInteger
RandomInteger()
Default constructor.
Definition: RandomInteger.java:46
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.random.RandomInteger.RandomInteger
RandomInteger(int min, int max, int seed, Random random)
Protected constructor used to duplicate the picker.
Definition: RandomInteger.java:71
ca.uqac.lif.synthia.random.RandomInteger.setInterval
RandomInteger setInterval(int min, int max)
Sets the interval in which integers are picked.
Definition: RandomInteger.java:84
ca.uqac.lif
ca.uqac.lif.synthia.random.Random.nextInt
int nextInt()
Returns the next pseudorandom, uniformly distributed.
Definition: Random.java:348
ca.uqac.lif.synthia.util.NothingPicker
A RelativePicker that only throws a NoMoreElementException when the pick() method is called.
Definition: NothingPicker.java:36
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.RandomInteger.setSeed
RandomInteger setSeed(int seed)
Definition: RandomInteger.java:102
ca.uqac.lif.synthia.random.RandomInteger
Picks an integer uniformly in an interval.
Definition: RandomInteger.java:31
ca.uqac.lif.synthia.random.RandomInteger.pick
Integer pick()
Picks a random integer.
Definition: RandomInteger.java:118
ca.uqac.lif.synthia.random.Random.Duplicate
Random Duplicate()
Creates a new instance of the class with the exact same internal states that the original one.
Definition: Random.java:123
ca
ca.uqac.lif.synthia.random.RandomInteger.duplicate
RandomInteger duplicate(boolean with_state)
Creates a copy of the RandomInteger picker.
Definition: RandomInteger.java:133
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.RandomFloat
Picks a floating point number uniformly in an interval.
Definition: RandomFloat.java:30
ca.uqac.lif.synthia.random.RandomInteger.tell
void tell(Integer max)
Sets the maximum bound for values of this picker.
Definition: RandomInteger.java:96
ca.uqac.lif.synthia.random.RandomPicker< Integer >::m_seed
int m_seed
Definition: RandomPicker.java:49
ca.uqac.lif.synthia.random.RandomInteger.shrink
Shrinkable< Integer > shrink(Integer element, Picker< Float > decision, float magnitude)
Definition: RandomInteger.java:144
ca.uqac.lif.synthia.random.RandomPicker< Integer >::m_random
transient Random m_random
Definition: RandomPicker.java:47
ca.uqac.lif.synthia.random.RandomInteger.toString
String toString()
Definition: RandomInteger.java:163
ca.uqac.lif.synthia.random.RandomInteger.m_max
int m_max
The higher bound of the interval.
Definition: RandomInteger.java:41