Question:
Java Array and random selection Help?
?
2011-02-03 20:30:05 UTC
I am having difficulties with what seems like a fairly simple idea.
public void randomBook() {
setAllBooksToNotRead();

int i;
book s;

boolean check=true;
while(check){
i = rand.nextInt(BookList.length);
if(BookList[i].getAlreadyread()==true){
check=false;
i = rand.nextInt(BookList.length);

}

System.out.println(BookList[i].getTitle());
BookList[i].setAlreadyread(true);

}
}
I am trying to make a program that randomly returns the titles of books without repeating them until the whole array has been "read". Assume that my instance fields are correct for the purposes of this question. I am stuck at this point, I am a beginner when it comes to java so I won't understand anything more than a simple array. My idea is that when a number is randomly called it should be removed from the array but I don't know how to do that.

thanks
Four answers:
Light Cloud
2011-02-03 20:38:19 UTC
If you want to randomly return titles of books without repeating, you are essentially asking for a permutation of the array.



An array with n elements has n! permutations. For instance, the array [1, 2, 3] has 3! = 6 permutations:

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1



The Java API has a built-in method that creates a permutation. It's the shuffle method in the Collections class:

http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collections.html#shuffle(java.util.List)



You could do something like:



List list = Arrays.asList(bookList);

Collections.shuffle(list);



// At this point, list will contain a permutation of the array; each permutation with equal chance

// You can just iterate through the array and print out the titles; e.g.

for (Book b : list)

System.out.println(b);





EDIT: And if you want to write the method yourself, the shuffle method basically works like this:

Given an array of n elements, we randomly select one of the n elements to be the last element. Swap this chosen element with the current last element. (So for instance, if the array had 10 elements and we randomly chose array[6], we would swap array[6] with array[9].) Afterwards, we would select one of the remaining n-1 elements, and swap it into the 2nd to last index. (So for instance, if we then chose array[3], we would swap array[3] with array[8].) And so forth. We do this for each element until we reach the beginning of the array. At this point, we will have created a permutation of the array.



You could also just try removing the element from the array once you've read it out, but this is less efficient, because in order to remove an element, you have to shift all the other elements above it down by one.
tristan c
2011-02-03 20:41:38 UTC
To remove the selected element from the array you can do one of two things:



Copy all elements in the array, except the selected element, into a new array.



-- or --



Replace the selected element with the last element in your array, then treat your array as if its size is one smaller.





The second method will be much faster, but it requires you you keep track of the number of unread books. The number of unread books will be the size you treat your array as.
?
2016-12-11 11:08:28 UTC
a million) Create an ArrayList (we could say it A) and positioned your ten thousand random numbers in it. Create an ArrayList (we could say it B) with empty length. 2) pick an integer (we could say it ok) between 0 and A.length()-a million, get ok. ingredient of A, positioned it in B , delete ok. ingredient of A. 3) do step 2 for a hundred situations. Now you have a hundred randomly factors of ten thousand . repeat comparable steps for extracting 5 factors from a hundred factors via bobbing up a sparkling ArrayList (C)..
anonymous
2016-11-29 05:45:58 UTC
a million) Create an ArrayList (we could say it A) and placed your ten thousand random numbers in it. Create an ArrayList (we could say it B) with empty length. 2) go with an integer (we could say it ok) between 0 and A.length()-a million, get ok. element of A, placed it in B , delete ok. element of A. 3) do step 2 for a hundred cases. Now you have a hundred randomly components of ten thousand . repeat same steps for extracting 5 components from a hundred components by capacity of coming up a clean ArrayList (C)..


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...