| Author |
Random array string that doesn't repeat
|
Kevin Corre
Greenhorn
Joined: Oct 26, 2011
Posts: 18
|
|
I have a string array with 30 items in it. I would like to randomly pick 10 and have them not repeat.
Thanks,
Kevin
|
 |
Aditya Jha
Ranch Hand
Joined: Aug 25, 2003
Posts: 227
|
|
Ok.
And what have you programmed so far? Can you post some code and the output you are getting?
|
 |
Kevin Corre
Greenhorn
Joined: Oct 26, 2011
Posts: 18
|
|
yes sorry wasn't sure if it was going to be needed. everything works it's just I get repeats when it pulls from the array.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4905
|
|
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.
Well first off, rather than an array, you might want to consider a List (java.util.List) or a Set (java.util.Set) of Strings. You might also want to have a look at Collections.shuffle().
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
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.
|
 |
Kevin Corre
Greenhorn
Joined: Oct 26, 2011
Posts: 18
|
|
|
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.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
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.
Of course. The upper bound should always be list.size(), giving you a number between 0 (inclusive) and list.size() (exclusive).
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4905
|
|
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.
Well if you had 30 cards and wanted to pick 10 of them at random, one way you might do it would be to shuffle the deck and deal 10 cards off the top.
Winston
|
 |
Kevin Corre
Greenhorn
Joined: Oct 26, 2011
Posts: 18
|
|
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?
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
|
Yes, you "have things in the wrong spot". But regardless of the fact that your code is supposed to be doing random things, it doesn't follow that putting lines of code into random places is a good way to write that code. Don't do that. Figure out what your code is supposed to do, and in what order. Then put the lines of code in that order.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
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?
Clearly if it doesn't do what you want, you wrote it wrong.
What you need to do is throw all this code away. Then sit down with pencil and paper (or the electronic equivalent), and write down IN ENGLISH how you would do this, step by step, without a computer. Then you revise that list. Then you revise it again, and again.
Only when you can look at each and every step on paper and say "oh, I know how to write that in Java" should you consider starting to write any Java. A good developer spends more time THINKKING about how/what to code than actually writing code. by a LONG chalk.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4905
|
|
Kevin Corre wrote:Well I tried adding some stuff from shuffle but it still comes up with repeats....
By 'adding some stuff from shuffle', I assume you mean copying the code, which is the antithesis of Object-orientation.
There's nothing wrong with plagiarisim in the OO world but, to be done properly, you use the original, NOT a copy, viz:Do you see what's happened? Pure plagiarism.
Not an original thought. Not a line of code that's actually doing anything that wasn't already written (and tested) by millions of others before me.
The only original part (and believe me, the above is NOT; but it can be) is how you put it together.
Perhaps that's why they call us software engineers.
Winston
|
 |
Kevin Corre
Greenhorn
Joined: Oct 26, 2011
Posts: 18
|
|
To Fred, no I don't need to throw away all of the code. The program was working fine before it was just repeating so I was trying to fix that. The only code I need to throw out is the code I just added and fix that.
To Winston, as I had stated in an earlier post
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.
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"
Kevin
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 6109
|
|
Kevin Corre wrote:To Fred, no I don't need to throw away all of the code.
At the very least, you need to throw away the parallel arrays and define a class.
|
 |
 |
|
|
subject: Random array string that doesn't repeat
|
|
|