Synthia
Generic and flexible data structure generator
RandomBoolean.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;
26 
27 /**
28  * Picks a Boolean value. This class actually implements a Bernoulli
29  * trial, with the possibility of setting the probability of success
30  * <i>p</i>.
31  *
32  * @ingroup API
33  */
34 public class RandomBoolean extends RandomPicker<Boolean> implements Shrinkable<Boolean>, Reactive<Float,Boolean>
35 {
36  /**
37  * The probability of picking <tt>true</tt>
38  */
39  protected float m_trueProbability;
40 
41  /**
42  * Creates a new instance of the picker
43  * @param true_probability The probability of picking <tt>true</tt>;
44  * must be between 0 and 1.
45  */
46  public RandomBoolean(/*@ non_null @*/ Number true_probability)
47  {
48  super();
49  m_trueProbability = true_probability.floatValue();
50  }
51 
52  /**
53  * Private constructor used in the duplicate method.
54  * @param true_probability The probability of picking <tt>true</tt>.
55  * @param seed The initial seed of the random genarator.
56  * @param random The random generator.
57  */
58  private RandomBoolean(Number true_probability, int seed, Random random)
59  {
60  m_trueProbability = true_probability.floatValue();
61  m_seed = seed;
62  m_random = random;
63  }
64 
65  /**
66  * Creates a new instance of the picker, with a 50-50 chance of
67  * producing <tt>true</tt> or <tt>false</tt>
68  */
69  public RandomBoolean()
70  {
71  this(0.5);
72  }
73 
74  /**
75  * Sets the probability for this picker to return <tt>true</tt>.
76  * @param p The probability of picking <tt>true</tt>.
77  */
78  @Override
79  public void tell(Float p)
80  {
82  }
83 
84  /**
85  * Picks a random boolean. Typically, this method is expected to return non-null
86  * objects; a <tt>null</tt> return value is used to signal that no more
87  * objects will be produced. That is, once this method returns
88  * <tt>null</tt>, it should normally return <tt>null</tt> on all subsequent
89  * calls.
90  * @return The random boolean.
91  */
92  @Override
93  public Boolean pick()
94  {
96  }
97 
98 
99  /**
100  * Creates a copy of the RandomBoolean picker.
101  * @param with_state If set to <tt>false</tt>, the returned copy is set to
102  * the class' initial state (i.e. same thing as calling the picker's
103  * constructor). If set to <tt>true</tt>, the returned copy is put into the
104  * same internal state as the object it is copied from.
105  * @return The copy of the RandomBoolean picker
106  */
107  @Override
108  public RandomBoolean duplicate(boolean with_state)
109  {
111 
112  if (!with_state)
113  {
114  copy.reset();
115  }
116 
117  return copy;
118  }
119 
120  @Override
121  public Shrinkable<Boolean> shrink(Boolean o, Picker<Float> decision, float magnitude)
122  {
123  // We assume an ordering of Booleans where false < true
124  if (o)
125  {
126  return new Constant<Boolean>(false);
127  }
128  return new NothingPicker<Boolean>();
129  }
130 
131  @Override
132  public Shrinkable<Boolean> shrink(Boolean o)
133  {
134  return shrink(o, RandomFloat.instance, 1);
135  }
136 }
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.Reactive
Interface implemented by pickers whose picking of objects can be altered by external information.
Definition: Reactive.java:43
ca.uqac.lif.synthia.util
Miscellaneous pickers performing various functions.
Definition: ArrayPicker.java:19
ca.uqac.lif.synthia.random.RandomBoolean.tell
void tell(Float p)
Sets the probability for this picker to return true.
Definition: RandomBoolean.java:79
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.RandomBoolean.m_trueProbability
float m_trueProbability
The probability of picking true
Definition: RandomBoolean.java:39
ca.uqac
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.util.Constant
Picker that returns the same object every time.
Definition: Constant.java:37
ca.uqac.lif.synthia.random.RandomBoolean.shrink
Shrinkable< Boolean > shrink(Boolean o, Picker< Float > decision, float magnitude)
Definition: RandomBoolean.java:121
ca.uqac.lif.synthia.random.RandomBoolean.RandomBoolean
RandomBoolean()
Creates a new instance of the picker, with a 50-50 chance of producing true or false
Definition: RandomBoolean.java:69
ca.uqac.lif.synthia.random.RandomBoolean.pick
Boolean pick()
Picks a random boolean.
Definition: RandomBoolean.java:93
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.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.RandomBoolean.duplicate
RandomBoolean duplicate(boolean with_state)
Creates a copy of the RandomBoolean picker.
Definition: RandomBoolean.java:108
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.RandomBoolean.shrink
Shrinkable< Boolean > shrink(Boolean o)
Definition: RandomBoolean.java:132
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< Boolean >::m_seed
int m_seed
Definition: RandomPicker.java:49
ca.uqac.lif.synthia.random.RandomPicker< Boolean >::m_random
transient Random m_random
Definition: RandomPicker.java:47
ca.uqac.lif.synthia.random.RandomBoolean.RandomBoolean
RandomBoolean(Number true_probability)
Creates a new instance of the picker.
Definition: RandomBoolean.java:46
ca.uqac.lif.synthia.random.RandomBoolean
Picks a Boolean value.
Definition: RandomBoolean.java:34