java - RandomIterator not producing random outputs -
public static void main(string args[]) { // build queue containing integers 1,2,...,6: randomizedqueue<integer> q= new randomizedqueue<integer>(); (int = 1; < 7; ++i) q.enqueue(i); // autoboxing! cool! // print 30 die rolls standard output stdout.print("some die rolls: "); (int = 1; < 30; ++i) stdout.print(q.sample() +" "); stdout.println(); // let's more serious: behave die rolls? int[] rolls= new int [10000]; (int = 0; < 10000; ++i) rolls[i] = q.sample(); // autounboxing! cool! stdout.printf("mean (should around 3.5): %5.4f\n", stdstats.mean(rolls)); stdout.printf("standard deviation (should around 1.7): %5.4f\n", stdstats.stddev(rolls)); // let's @ iterator. first, make 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 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 2 colours first shuffle: "); stdout.print(i.next()+" "); stdout.println(i.next()); }
i had borrowed client somewhere check randomizedqueue implementation . else works iterator part of not working . mean sample output 1 run :
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 around 3.5): 3.4882 standard deviation (should around 1.7): 1.7069 2 colours 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 2 colours first shuffle: blue red
iteration specific iterator should random on objects in randomizedqueue , each iterator should remember it's own specific order ( once declared ) . here code 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() { = (item[]) new object[2]; } // construct empty randomized queue public boolean isempty() { return n == 0; } // queue empty? public int size() { return n; } // return number of items on 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 item private void resize(int capacity) { assert capacity >= n; item[] temp = (item[]) new object[capacity]; for(int = 0; < n; i++) { temp[i] = a[i]; } = temp; } public item dequeue() { if(isempty()) throw new java.util.nosuchelementexception(); int random = stdrandom.uniform(n); // generating number b/w 0 , n-1 // swap 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 , return random item public item sample() { if(isempty()) throw new java.util.nosuchelementexception(); int random = stdrandom.uniform(n); // generating number b/w 0 , n-1 item item = a[random]; return item; } // return (but not delete) random item public iterator<item> iterator() { return new randomiterator(); } // return independent iterator on items in random order private class randomiterator implements iterator<item> { private int = 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 > 0 ;} public void remove() { throw new unsupportedoperationexception(); } public item next() { if(!hasnext()) throw new nosuchelementexception(); return a[--i]; } } }
stdin , stdout , stdrandom classes provided assignment functions have been used in program , work name suggests . why not different iterators producing random outputs. shuffle
produces random permutation of objects . arraycopy copies 1 array . stdout.print
behaves system.out.print
it looks correct me, let's check accuracy
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 around 3.5): 3.4882 standard deviation (should around 1.7): 1.7069
3.5 - 3.4882
= 0.0118
and
1.7 - 1.7069
= -0.069
both "around" expected values.
edit
also,
stdrandom.shuffle(itemcopy, 0, n-1);
should be
stdrandom.shuffle(itemcopy, 0, n);
also, share same iterator()
way you're doing it
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();
Comments
Post a Comment