RandomIterator不产生随机输出

时间:2014-06-27 07:09:22

标签: java random iterator

public static void main(String args[])
{
    // Build a queue containing the Integers 1,2,...,6:
    RandomizedQueue<Integer> Q= new RandomizedQueue<Integer>();
    for (int i = 1; i < 7; ++i) Q.enqueue(i); // autoboxing! cool!

    // Print 30 die rolls to standard output
    StdOut.print("Some die rolls: ");
    for (int i = 1; i < 30; ++i) StdOut.print(Q.sample() +" ");
    StdOut.println();

    // Let's be more serious: do they really behave like die rolls?
    int[] rolls= new int [10000];
    for (int i = 0; i < 10000; ++i)
        rolls[i] = Q.sample(); // autounboxing! Also cool!
    StdOut.printf("Mean (should be around 3.5): %5.4f\n", StdStats.mean(rolls));
    StdOut.printf("Standard deviation (should be around 1.7): %5.4f\n",
                  StdStats.stddev(rolls));

    // Let's look at the iterator. First, we make a queue of colours:

    RandomizedQueue<String> C= new RandomizedQueue<String>();
    C.enqueue("red"); C.enqueue("blue"); C.enqueue("green"); C.enqueue("yellow"); 

    Iterator I= C.iterator();
    Iterator J= C.iterator();
    Iterator K= C.iterator();
    Iterator L= C.iterator();
    Iterator M= C.iterator();
    Iterator N= C.iterator();


    StdOut.print("Two colours from first shuffle: ");
    StdOut.print(I.next()+" ");
    StdOut.print(I.next()+" ");

    StdOut.print("\nEntire second shuffle: ");
    while (J.hasNext()) StdOut.print(J.next()+" ");

    StdOut.print("\nEntire third shuffle: ");
    while (K.hasNext()) StdOut.print(K.next()+" ");

    StdOut.print("\nEntire fourth shuffle: ");
    while (L.hasNext()) StdOut.print(L.next()+" ");

    StdOut.print("\nEntire fifth shuffle: ");
    while (M.hasNext()) StdOut.print(M.next()+" ");

    StdOut.print("\nEntire sixth shuffle: ");
    while (N.hasNext()) StdOut.print(N.next()+" ");

    StdOut.print("\nRemaining two colours from first shuffle: ");
    StdOut.print(I.next()+" ");
    StdOut.println(I.next());
}

我从某个地方借过这个客户端来检查我的RandomizedQueue实现。其他一切都有效,但迭代器部分无效。我的意思是一次运行的示例输出是:

Some die rolls: 1 1 5 6 3 6 6 1 2 4 6 2 4 5 6 4 4 2 2 5 6 6 2 6 4 5 3 3 2 
Mean (should be around 3.5): 3.4882
Standard deviation (should be around 1.7): 1.7069
Two colours from first shuffle: yellow green 
Entire second shuffle: yellow green blue red 
Entire third shuffle: yellow green blue red 
Entire fourth shuffle: yellow green blue red 
Entire fifth shuffle: yellow green blue red 
Entire sixth shuffle: yellow green blue red 
Remaining two colours from first shuffle: blue red

特定迭代器的迭代应该是RandomizedQueue中对象的随机变量,每个迭代器应该记住它自己的特定顺序(一旦声明)。 这是我的Iterable和Iterator的代码:

import java.util.Iterator;
import java.util.NoSuchElementException;
public class RandomizedQueue<Item> implements Iterable<Item> {
    private Item[] a;
    private int N;
    public RandomizedQueue() {
        a = (Item[]) new Object[2];
    }                // construct an empty randomized queue
    public boolean isEmpty() {
        return N == 0; 
    }               // is the queue empty?
    public int size() {
        return N;
    }                        // return the number of items on the queue
    public void enqueue(Item item) {
        if (item == null) throw new NullPointerException();
        if (N == a.length) resize(2*a.length);    // double size of array if necessary
        a[N++] = item;
    }           // add the item
    private void resize(int capacity) {
        assert capacity >= N;
        Item[] temp = (Item[]) new Object[capacity];
        for(int i = 0; i < N; i++) {
            temp[i] = a[i];
        }
        a = temp;
    }
    public Item dequeue() {
        if(isEmpty()) throw new java.util.NoSuchElementException();
        int random = StdRandom.uniform(N);  // generating a number b/w 0 and N-1
        // swap with the final element
        Item temp = a[random];
        a[random] = a[N-1];
        a[N-1] = temp;

        Item item = a[N-1];
        a[N-1] = null;
        N--;

        if (N > 0 && N == a.length/4) resize(a.length/2);
        return item;
    }                   // delete and return a random item
    public Item sample() {
        if(isEmpty()) throw new java.util.NoSuchElementException();
        int random = StdRandom.uniform(N);  // generating a number b/w 0 and N-1
        Item item = a[random];
        return item;
    }                    // return (but do not delete) a random item
    public Iterator<Item> iterator() {
        return new RandomIterator();
    }        // return an independent iterator over items in random order
    private class RandomIterator implements Iterator<Item> {
        private int i = N;
        private Item[] itemcopy = (Item[]) new Object[N];
        public RandomIterator() {
           System.arraycopy(a, 0, itemcopy, 0, N);
           StdRandom.shuffle(itemcopy, 0, N-1);
        }
        public boolean hasNext() { return i > 0 ;}
        public void remove()     { throw new UnsupportedOperationException(); } 
        public Item next()   
        { 
            if(!hasNext()) throw new NoSuchElementException();
            return a[--i];
        }
    }
}

StdIn,StdOut和StdRandom是为作业提供的类,它们的功能已经在程序中使用,它们的名称就是它们的作用。 那么为什么不同的迭代器产生随机输出呢? shuffle生成对象的随机排列。 arraycopy将一个数组复制到另一个数组。 StdOut.print的行为与System.out.print

类似

1 个答案:

答案 0 :(得分:2)

对我而言,让我们检查一下准确性

Some die rolls: 1 1 5 6 3 6 6 1 2 4 6 2 4 5 6 4 4 2 2 5 6 6 2 6 4 5 3 3 2 
Mean (should be around 3.5): 3.4882
Standard deviation (should be around 1.7): 1.7069

3.5 - 3.4882 = 0.0118

1.7 - 1.7069 = -0.069

两者都在“预期值”附近。

修改

另外,

StdRandom.shuffle(itemcopy, 0, N-1);

应该是

StdRandom.shuffle(itemcopy, 0, N);

此外,您按照您的方式共享iterator()

RandomizedQueue<String> C= new RandomizedQueue<String>();
C.enqueue("red"); C.enqueue("blue"); C.enqueue("green"); C.enqueue("yellow"); 

Iterator I= new RandomizedQueue<String>(C).iterator();
Iterator J= new RandomizedQueue<String>(C).iterator();
Iterator K= new RandomizedQueue<String>(C).iterator();
Iterator L= new RandomizedQueue<String>(C).iterator();
Iterator M= new RandomizedQueue<String>(C).iterator();
Iterator N= new RandomizedQueue<String>(C).iterator();