Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //AdaptRandDoubles.java
- package my.eckel;
- import java.nio.*;
- import java.util.*;
- public class AdaptRandDoubles extends RandDoubles implements Readable {
- private int count;
- public AdaptRandDoubles(int count) {
- this.count = count;
- }
- @Override
- public int read(CharBuffer cb) {
- if (count-- == 0)
- return -1;
- String result = Double.toString(next()) + " ";
- cb.append(result);
- return result.length();
- }
- public static void main(String[] args) {
- Scanner s = new Scanner(new AdaptRandDoubles(2));
- while (s.hasNextDouble())
- System.out.println(s.nextDouble() + " ");
- }
- }
- //------------------------------------------------------------
- //RandDoubles.java
- package my.eckel;
- import java.util.*;
- public class RandDoubles {
- private static Random rand = new Random(47);
- public double next() { return rand.nextDouble(); }
- public static void main(String[] args) {
- RandDoubles rd = new RandDoubles();
- for (int i = 0; i < 7; i++)
- System.out.println(rd.next() + " ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment