Synthia
Generic and flexible data structure generator
BiasedCoin.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 
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 examples.basic;
20 
21 import ca.uqac.lif.synthia.Picker;
23 
24 /**
25  * Uses {@link RandomBoolean} to simulate a series of biased coin tosses.
26  * The program shows the number of times the picker produces "heads"
27  * (i.e. true) and compares this to the expected number of heads as
28  * dictated by the probability. A possible run of the program is:
29  * <pre>
30  * Flips Heads Expected
31  * 100 31 30
32  * 200 58 60
33  * 300 84 90
34  * 400 119 120
35  * 500 155 150
36  * 600 188 180
37  * 700 218 210
38  * 800 251 240
39  * 900 282 270
40  * 1000 312 300
41  * </pre>
42  * @author Sylvain Hallé
43  * @ingroup Examples
44  */
45 public class BiasedCoin
46 {
47  public static void main(String[] args)
48  {
49  /* The probability of obtaining true (i.e. heads). */
50  float p = 0.3f;
51 
52  /* Create a Boolean picker with this probability, call its pick() method
53  * 1,000 times, and show how many times the value true has been observed
54  * every 100 events. */
55  int num_heads = 0;
56  System.out.println("Flips\tHeads\tExpected");
57  Picker<Boolean> coin = new RandomBoolean(p).setSeed(0);
58  for (int i = 1; i <= 10; i++)
59  {
60  for (int j = 1; j <= 100; j++)
61  num_heads += coin.pick() ? 1 : 0;
62  System.out.printf("%d\t%d\t%d\n", 100 * i, num_heads, 100 * i * p);
63  }
64  }
65 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac
ca.uqac.lif.synthia.random
Pickers that produce pseudo-random objects such as numbers.
Definition: AffineTransform.java:19
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif
ca
examples.basic.BiasedCoin.main
static void main(String[] args)
Definition: BiasedCoin.java:47
ca.uqac.lif.synthia.Picker.pick
T pick()
Picks an object.
ca.uqac.lif.synthia.random.RandomPicker.setSeed
RandomPicker< T > setSeed(int seed)
Set the seed of the random generator.
Definition: RandomPicker.java:60
examples.basic.BiasedCoin
Uses RandomBoolean to simulate a series of biased coin tosses.
Definition: BiasedCoin.java:45
ca.uqac.lif.synthia.random.RandomBoolean
Picks a Boolean value.
Definition: RandomBoolean.java:34