SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Kevin Corre wrote:I have a string array with 30 items in it. I would like to randomly pick 10 and have them not repeat.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Rob Spoor wrote:One way is to put the Strings in a List, and then remove a random element each time. Because a String will not be in the List anymore afterwards it cannot be returned by a next attempt.
John Jai wrote:
Rob Spoor wrote:One way is to put the Strings in a List, and then remove a random element each time. Because a String will not be in the List anymore afterwards it cannot be returned by a next attempt.
Just adding that you also need to check and decrement the value of n (the upper bound) you have to create random numbers as you remove elements from the List.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Kevin Corre wrote:I had looked at the collections and shuffle but not familiar with this so I wasn't sure how to implement it into the code.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Kevin Corre wrote:Well I tried adding some stuff from shuffle but it still comes up with repeats. Here is the updated code. Do I have things in the wrong spot or just wrote it wrong?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Kevin Corre wrote:Well I tried adding some stuff from shuffle but it still comes up with repeats....
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
With that I was looking for some direction in how to do it and write the code because I didn't know. I had done some reading and searching and yes I had found some code that was written before by someone else and changed it to fit what I thought I needed. Sorry I didn't use the right terminology with saying "Well I added some stuff"I had looked at the collections and shuffle but not familiar with this so I wasn't sure how to implement it into the code.
Kevin Corre wrote:To Fred, no I don't need to throw away all of the code.
Consider Paul's rocket mass heater. |