• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random array string that doesn't repeat  RSS feed

 
Kevin Corre
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.

And what have you programmed so far? Can you post some code and the output you are getting?
 
Kevin Corre
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!