gpt4 book ai didi

java - RandomIterator 不产生随机输出

转载 作者:行者123 更新时间:2023-11-30 03:51:04 24 4
gpt4 key购买 nike

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

类似

最佳答案

我认为它是正确的,让我们检查一下准确性

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();

关于java - RandomIterator 不产生随机输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24445963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com