KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > exslt > Random


1 package net.sf.saxon.exslt;
2
3 import net.sf.saxon.om.AxisIteratorImpl;
4 import net.sf.saxon.om.Item;
5 import net.sf.saxon.om.SequenceIterator;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.value.DoubleValue;
8
9 /**
10  * This class implements extension functions in the
11  * http://exslt.org/random namespace.
12  *
13  * @author Martin Szugat
14  * @version 1.0, 30.06.2004
15  * Rewritten by Michael Kay to generate a SequenceIterator
16  */

17 public abstract class Random {
18
19     /**
20      * Returns a sequence of random numbers
21      * between 0 and 1.
22      * @param numberOfItems number of random items
23      * in the sequence.
24      * @param seed the initial seed.
25      * @return sequence of random numbers as an iterator.
26      * @throws IllegalArgumentException
27      * <code>numberOfItems</code> is not positive.
28      */

29     public static SequenceIterator randomSequence(int numberOfItems, double seed)
30     throws IllegalArgumentException JavaDoc {
31         if (numberOfItems < 1) {
32             throw new IllegalArgumentException JavaDoc("numberOfItems supplied to randomSequence() must be positive");
33         }
34         long javaSeed = Double.doubleToLongBits(seed);
35         return new RandomIterator(numberOfItems, javaSeed);
36     }
37
38     /**
39      * Returns a sequence of random numbers
40      * between 0 and 1.
41      * @param numberOfItems number of random items
42      * in the sequence.
43      * @return sequence of random numbers.
44      * @throws IllegalArgumentException
45      * <code>numberOfItems</code> is not positive.
46      */

47     public static SequenceIterator randomSequence(int numberOfItems)
48     throws IllegalArgumentException JavaDoc {
49         return randomSequence(numberOfItems, System.currentTimeMillis());
50     }
51
52     /**
53      * Returns a single random number X
54      * between 0 and 1.
55      * @return sequence random number.
56      */

57     public static DoubleValue randomSequence() throws XPathException {
58         return (DoubleValue)randomSequence(1).next();
59     }
60
61     /**
62      * Iterator over a sequence of random numbers
63      */

64
65     private static class RandomIterator extends AxisIteratorImpl {
66
67         private int count;
68         private long seed;
69         private java.util.Random JavaDoc generator;
70
71         public RandomIterator(int count, long seed) {
72             this.count = count;
73             this.seed = seed;
74             generator = new java.util.Random JavaDoc(seed);
75         }
76
77         /**
78          * Get the next item in the sequence. <BR>
79          * @return the next item, or null if there are no more items.
80          */

81
82         public Item next() {
83             if (position++ >= count) {
84                 current = null;
85                 position = -1;
86                 return null;
87             } else {
88                 current = new DoubleValue(generator.nextDouble());
89                 return current;
90             }
91         }
92
93         /**
94          * Get another SequenceIterator that iterates over the same items as the original,
95          * but which is repositioned at the start of the sequence.
96          *
97          * @return a SequenceIterator that iterates over the same items,
98          * positioned before the first item
99          */

100
101         public SequenceIterator getAnother() {
102             return new RandomIterator(count, seed);
103         }
104     }
105 }
106
107 //
108
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
109
// you may not use this file except in compliance with the License. You may obtain a copy of the
110
// License at http://www.mozilla.org/MPL/
111
//
112
// Software distributed under the License is distributed on an "AS IS" basis,
113
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
114
// See the License for the specific language governing rights and limitations under the License.
115
//
116
// The Original Code is: all this file.
117
//
118
// The Initial Developer of the Original Code is Martin Szugat.
119
//
120
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
121
//
122
// Contributor(s): Michael H. Kay.
123
//
Popular Tags