Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Random array string that doesn't repeat

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 22665
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22665
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Marshal
Posts: 27288
87
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13063
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic