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 17 public abstract class Random { 18 19 29 public static SequenceIterator randomSequence(int numberOfItems, double seed) 30 throws IllegalArgumentException { 31 if (numberOfItems < 1) { 32 throw new IllegalArgumentException ("numberOfItems supplied to randomSequence() must be positive"); 33 } 34 long javaSeed = Double.doubleToLongBits(seed); 35 return new RandomIterator(numberOfItems, javaSeed); 36 } 37 38 47 public static SequenceIterator randomSequence(int numberOfItems) 48 throws IllegalArgumentException { 49 return randomSequence(numberOfItems, System.currentTimeMillis()); 50 } 51 52 57 public static DoubleValue randomSequence() throws XPathException { 58 return (DoubleValue)randomSequence(1).next(); 59 } 60 61 64 65 private static class RandomIterator extends AxisIteratorImpl { 66 67 private int count; 68 private long seed; 69 private java.util.Random generator; 70 71 public RandomIterator(int count, long seed) { 72 this.count = count; 73 this.seed = seed; 74 generator = new java.util.Random (seed); 75 } 76 77 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 100 101 public SequenceIterator getAnother() { 102 return new RandomIterator(count, seed); 103 } 104 } 105 } 106 107 | Popular Tags |