Synthia
Generic and flexible data structure generator
RandomFloat.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;
24 
25 /**
26  * Picks a floating point number uniformly in an interval.
27  *
28  * @ingroup API
29  */
30 public class RandomFloat extends RandomPicker<Float> implements Shrinkable<Float>
31 {
32  /**
33  * The lower bound of the interval
34  */
35  protected float m_min;
36 
37  /**
38  * The higher bound of the interval
39  */
40  protected float m_max;
41 
42  /**
43  * A public static instance of RandomFloat.
44  */
45  public static final transient RandomFloat instance = new RandomFloat().setSeed(0);
46 
47  /**
48  * Default constructor.
49  */
50  public RandomFloat()
51  {
52  super();
53  m_min = 0;
54  m_max = 1;
55  }
56 
57  /**
58  * Creates a new instance of the picker.
59  * @param min The lower bound of the interval
60  * @param max The higher bound of the interval
61  */
62  public RandomFloat(Number min, Number max)
63  {
64  super();
65  m_min = min.floatValue();
66  m_max = max.floatValue();
67  }
68 
69  /**
70  * Private constructor used to duplicate the picker.
71  * @param min The lower bound of the interval.
72  * @param max The higher bound of the interval.
73  * @param seed The initial seed of the random genarator.
74  * @param random The random generator.
75  */
76  private RandomFloat(Number min, Number max, int seed, Random random)
77  {
78  m_min = min.floatValue();
79  m_max = max.floatValue();
80  m_seed = seed;
81  m_random = random;
82  }
83 
84  @Override
85  public RandomFloat setSeed(int seed)
86  {
87  super.setSeed(seed);
88  return this;
89  }
90 
91  /**
92  * Picks a random float in the specified interval. Typically, this method is expected to return non-null
93  * objects; a <tt>null</tt> return value is used to signal that no more
94  * objects will be produced. That is, once this method returns
95  * <tt>null</tt>, it should normally return <tt>null</tt> on all subsequent
96  * calls.
97  * @return The random float
98  */
99  @Override
100  public Float pick()
101  {
102  return m_random.nextFloat() * (m_max - m_min) + m_min;
103  }
104 
105  /**
106  * Creates a copy of the RandomIntervalFloat picker.
107  * @param with_state If set to <tt>false</tt>, the returned copy is set to
108  * the class' initial state (i.e. same thing as calling the picker's
109  * constructor). If set to <tt>true</tt>, the returned copy is put into the
110  * same internal state as the object it is copied from.
111  * @return The copy of the RandomIntervalFloat picker
112  */
113  @Override
114  public RandomFloat duplicate(boolean with_state)
115  {
117 
118  if (!with_state)
119  {
120  copy.reset();
121  }
122 
123  return copy;
124  }
125 
126  @Override
127  public Shrinkable<Float> shrink(Float element)
128  {
129  return shrink(element, instance, 1);
130  }
131 
132  @Override
133  public Shrinkable<Float> shrink(Float element, Picker<Float> decision, float magnitude)
134  {
135  if((element <= m_min) || (element.isNaN()))
136  {
137 
138  return new NothingPicker<Float>();
139 
140  }
141  else
142  {
143  return new RandomFloat(m_min, element);
144  }
145  }
146 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.Shrinkable
Interface signaling that a picker can be shrunk.
Definition: Shrinkable.java:36
ca.uqac.lif.synthia.util
Miscellaneous pickers performing various functions.
Definition: ArrayPicker.java:19
ca.uqac.lif.synthia.random.RandomFloat.RandomFloat
RandomFloat(Number min, Number max)
Creates a new instance of the picker.
Definition: RandomFloat.java:62
ca.uqac.lif.synthia.random.RandomFloat.instance
static final transient RandomFloat instance
A public static instance of RandomFloat.
Definition: RandomFloat.java:45
ca.uqac
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.random.RandomFloat.RandomFloat
RandomFloat()
Default constructor.
Definition: RandomFloat.java:50
ca.uqac.lif.synthia.random.RandomFloat.duplicate
RandomFloat duplicate(boolean with_state)
Creates a copy of the RandomIntervalFloat picker.
Definition: RandomFloat.java:114
ca.uqac.lif.synthia.random.RandomFloat.shrink
Shrinkable< Float > shrink(Float element, Picker< Float > decision, float magnitude)
Definition: RandomFloat.java:133
ca.uqac.lif.synthia.random.RandomFloat.pick
Float pick()
Picks a random float in the specified interval.
Definition: RandomFloat.java:100
ca.uqac.lif.synthia.random.RandomFloat.setSeed
RandomFloat setSeed(int seed)
Definition: RandomFloat.java:85
ca.uqac.lif
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.RandomFloat.m_max
float m_max
The higher bound of the interval.
Definition: RandomFloat.java:40
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.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.shrink
Shrinkable< Float > shrink(Float element)
Definition: RandomFloat.java:127
ca.uqac.lif.synthia.random.RandomFloat.m_min
float m_min
The lower bound of the interval.
Definition: RandomFloat.java:35
ca.uqac.lif.synthia.random.Random.nextFloat
float nextFloat()
Returns the next pseudorandom, uniformly distributed.
Definition: Random.java:508
ca.uqac.lif.synthia.random.RandomFloat
Picks a floating point number uniformly in an interval.
Definition: RandomFloat.java:30
ca.uqac.lif.synthia.random.RandomPicker< Float >::m_seed
int m_seed
Definition: RandomPicker.java:49
ca.uqac.lif.synthia.random.RandomPicker< Float >::m_random
transient Random m_random
Definition: RandomPicker.java:47