Skip to content

Commit ef41a44

Browse files
authored
Merge pull request #59 from zhoupinheng/master
add triangular stream!
2 parents 9d3ccbf + 31580ee commit ef41a44

File tree

4 files changed

+220
-3
lines changed

4 files changed

+220
-3
lines changed

docs/manual.adoc

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,26 @@ NormalStream returns a normal distribution of random numbers, with mean mean and
489489
----
490490
public class NormalStream extends RandomStream
491491
{
492-
public NormalStream (double mean, double sd);
493-
public NormalStream (double mean, double sd, int StreamSelect);
494-
public NormalStream (double mean, double sd, int StreamSelect, long MGSeed, long LCGSeed);
492+
public NormalStream (double mean, double sd);
493+
public NormalStream (double mean, double sd, int StreamSelect);
494+
public NormalStream (double mean, double sd, int StreamSelect, long MGSeed, long LCGSeed);
495+
496+
public double getNumber () throws IOException, ArithmeticException;
497+
};
498+
----
499+
500+
StreamSelect indicates the offset in the random number sequence to begin sampling, and MGSeed and LCGSeed can be used to modify the seed values used by the RandomStream class.
501+
502+
=== TriangularStream
503+
504+
TriangularStream returns a triangular distribution of random numbers, with lower limit a, upper limit b and mode c, where a < b and a ≤ c ≤ b.
505+
506+
----
507+
public class TriangularStream extends RandomStream
508+
{
509+
public NormalStream (double a, double b, double c);
510+
public NormalStream (double a, double b, double c, int StreamSelect);
511+
public NormalStream (double a, double b, double c, int StreamSelect, long MGSeed, long LCGSeed);
495512
496513
public double getNumber () throws IOException, ArithmeticException;
497514
};
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne
3+
* and others contributors as indicated
4+
* by the @authors tag. All rights reserved.
5+
* See the copyright.txt in the distribution for a
6+
* full listing of individual contributors.
7+
* This copyrighted material is made available to anyone wishing to use,
8+
* modify, copy, or redistribute it subject to the terms and conditions
9+
* of the GNU Lesser General Public License, v. 2.1.
10+
* This program is distributed in the hope that it will be useful, but WITHOUT A
11+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
12+
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
13+
* You should have received a copy of the GNU Lesser General Public License,
14+
* v.2.1 along with this distribution; if not, write to the Free Software
15+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16+
* MA 02110-1301, USA.
17+
*
18+
* (C) 1990-2008,
19+
*/
20+
21+
package org.javasim.streams;
22+
23+
import java.io.IOException;
24+
25+
/**
26+
* Returns a number drawn from a triangular distribution with lower limit a, upper limit b and mode c, where a < b and a ≤ c ≤ b.
27+
*/
28+
29+
public class TriangularStream extends RandomStream {
30+
/**
31+
* Create stream with low bound 'l'(a) and high bound 'h'(b) and 'm'(c) value.
32+
*/
33+
34+
public TriangularStream(double a, double b, double c) {
35+
super();
36+
37+
this.a = a;
38+
this.b = b;
39+
this.c = c;
40+
}
41+
42+
/**
43+
* Create stream with low bound 'l'(a) and high bound 'h'(b) and 'm'(c) value. Skip the first 'StreamSelect' values before returning numbers from the stream.
44+
*/
45+
46+
public TriangularStream(double a, double b, double c, int StreamSelect) {
47+
super();
48+
49+
this.a = a;
50+
this.b = b;
51+
this.c = c;
52+
53+
for (int i = 0; i < StreamSelect * 1000; i++)
54+
uniform();
55+
}
56+
57+
/**
58+
* Create stream with low bound 'l'(a) and high bound 'h'(b) and 'm'(c) value. Skip the first 'StreamSelect' values before returning numbers from the stream. Pass the seeds 'MGSeed' and 'LCGSeed' to
59+
* the base class.
60+
*/
61+
62+
public TriangularStream(double a, double b, double c, int StreamSelect, long MGSeed, long LCGSeed) {
63+
super(MGSeed, LCGSeed);
64+
65+
this.a = a;
66+
this.b = b;
67+
this.c = c;
68+
69+
for (int i = 0; i < StreamSelect * 1000; i++)
70+
uniform();
71+
}
72+
73+
/**
74+
* @return a number from the stream.
75+
*/
76+
77+
public double getNumber() throws IOException, ArithmeticException {
78+
79+
double F = (c - a) / (b - a);
80+
double rand = uniform();
81+
if (rand < F) {
82+
return a + Math.sqrt(rand * (b - a) * (c - a));
83+
} else {
84+
return b - Math.sqrt((1 - rand) * (b - a) * (b - c));
85+
}
86+
87+
}
88+
89+
private double a;
90+
private double b;
91+
private double c;
92+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne
3+
* and others contributors as indicated
4+
* by the @authors tag. All rights reserved.
5+
* See the copyright.txt in the distribution for a
6+
* full listing of individual contributors.
7+
* This copyrighted material is made available to anyone wishing to use,
8+
* modify, copy, or redistribute it subject to the terms and conditions
9+
* of the GNU Lesser General Public License, v. 2.1.
10+
* This program is distributed in the hope that it will be useful, but WITHOUT A
11+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
12+
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
13+
* You should have received a copy of the GNU Lesser General Public License,
14+
* v.2.1 along with this distribution; if not, write to the Free Software
15+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16+
* MA 02110-1301, USA.
17+
*
18+
* (C) 1990-2008,
19+
*/
20+
21+
package org.javasim.examples.streams;
22+
23+
import org.javasim.stats.Histogram;
24+
import org.javasim.streams.TriangularStream;
25+
import org.junit.Test;
26+
27+
public class TriangularExampleStreamTest {
28+
@Test
29+
public void test() {
30+
try {
31+
32+
TriangularStream triagular = new TriangularStream(0, 20, 7);
33+
34+
Histogram hist = new Histogram(25);
35+
36+
for (int i = 0; i < 10000; i++) {
37+
int value = (int) Math.round(triagular.getNumber());
38+
39+
System.out.println(" " + value);
40+
41+
hist.setValue(value);
42+
}
43+
44+
System.out.println("NormalStream error: " + triagular.error());
45+
46+
hist.print();
47+
48+
} catch (final Throwable ex) {
49+
}
50+
}
51+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne
3+
* and others contributors as indicated
4+
* by the @authors tag. All rights reserved.
5+
* See the copyright.txt in the distribution for a
6+
* full listing of individual contributors.
7+
* This copyrighted material is made available to anyone wishing to use,
8+
* modify, copy, or redistribute it subject to the terms and conditions
9+
* of the GNU Lesser General Public License, v. 2.1.
10+
* This program is distributed in the hope that it will be useful, but WITHOUT A
11+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
12+
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
13+
* You should have received a copy of the GNU Lesser General Public License,
14+
* v.2.1 along with this distribution; if not, write to the Free Software
15+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16+
* MA 02110-1301, USA.
17+
*
18+
* (C) 1990-2008,
19+
*/
20+
21+
package org.javasim.tests.streams;
22+
23+
import static org.junit.Assert.fail;
24+
25+
import org.javasim.streams.TriangularStream;
26+
import org.junit.Test;
27+
28+
public class TriangularStreamUnitTest {
29+
@Test
30+
public void test() throws Exception {
31+
TriangularStream str = new TriangularStream(0, 10, 6);
32+
33+
try {
34+
str.getNumber();
35+
str.error();
36+
} catch (final Exception ex) {
37+
fail();
38+
}
39+
40+
str = new TriangularStream(0, 10, 6, 1000);
41+
42+
try {
43+
str.getNumber();
44+
} catch (final Exception ex) {
45+
fail();
46+
}
47+
48+
str = new TriangularStream(0, 10, 6, 1000, 772532, 1878892441);
49+
50+
try {
51+
str.getNumber();
52+
str.error();
53+
} catch (final Exception ex) {
54+
fail();
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)