Solved How to get a random string out of String[]

Discussion in 'Plugin Development' started by CaptainDirt, Nov 30, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    CaptainDirt

    Hey all, I'm making a plugin where I need to grab lets say 5 random Strings out of a String []. I know it's something pretty basic but I'm just mind blank on the topic.
     
  2. Offline

    mrCookieSlime Retired Staff

    CaptainDirt
    You need to convert the array to an arraylist. You can do that by using Arrays.asList(array);
    And then just use the .get() method to retrieve a value.
    Now to get a random Value you can use new Random().nextInt(array.length);

    All you have to do is to combine the pieces.
     
    CaptainDirt likes this.
  3. Offline

    Zupsub

    You can also use Collection.shuffle() to shuffle your collection and get the first n elements then.
     
  4. Offline

    CaptainDirt

    Thanks, also, what other places would you recommend for learning Java? I obviously haven't fully learnt the Java language for the tutorials I used a while ago. (I started making plugins about 6 months ago)
     
  5. Offline

    mrCookieSlime Retired Staff

  6. Offline

    NonameSL

    Wow.. mrCookieSlime why'd he do that while there's a much simpler way?

    Create a set, and then while the set's length is smaller than the amount of strings you wanted to get you keep adding a random one. Like so:
    Code:Java
    1. Random random = new Random();
    2. HashSet<String> set = new HashSet<String>();
    3. while(set<5/*the amount of strings*/){
    4. set.add(array[random.nextInt(array.length)]);
    5. }
    6.  




    First learn java, then start making plugins. It is so much easier and simpler, plus, why would you learn an API for Java when you don't know java? It's like making coffee, putting it in a bowl, then getting the cup and putting the coffee in the bowl.
     
  7. Offline

    mrCookieSlime Retired Staff

    NonameSL
    *facepalm*
    Sorry for that. I meant
    array[new Random().nextInt(array.length)]
    instead of
    Arrays.asList(array).get(new Random().nextInt(array.length))

    No idea why I came up with the ArrayList idea :?

    Well, these sort of things tend to happen if you are in a rush and extremely tired while also being busy answering PMs etc...
     
  8. Offline

    Rocoty

    NonameSL This is inefficient, as it could - in theory - go on forever.
     
  9. Offline

    fireblast709

    NonameSL Assuming you would keep picking the same element (and Sets will ignore the addition of such a duplicate), you have an algorithm that theoretically takes infinite time. (note: this is from a formal point of view)

    mrCookieSlime your algorithm will run in O(n), which is the fastest presented thus far. For the rest who wonders how I get to this number:
    • It takes O(n) to copy the array into a List
    • Removing an element (thus preventing duplicate picks) takes O(1). Assuming a worst case of n elements, this takes O(n)
    • O(n) + O(n) = O(2n) (which is usually written down as O(n), since it runs linear - that's generally what matters)
     
    leon3001 and Rocoty like this.
  10. Offline

    BrentHogeling

  11. Offline

    ChipDev

    Oh my. Don't geek off on me plz fireblast709,
    Code:Java
    1.  
    2. Random random = new Random();
    3. int r = random.nextInt(yourstring[].length) + 1;
    4. String s = yourstring[r];
    5.  


    Written on iPhone. Sorry![/i]
     
  12. Offline

    mrCookieSlime Retired Staff

    ChipDev ChipDev
    I can already see the ArrayIndexOutOfBoundsException coming...
    The + 1 will cause errors.
     
  13. Offline

    ChipDev

    I thought that would cause it to start at 1.. What?

    I think I need to learn java again..
    Thanks for tagging me twice ;) I love alerts!!
     
    akabarblake likes this.
  14. Offline

    mrCookieSlime Retired Staff

    ChipDev likes this.
  15. Offline

    fireblast709

    ChipDev now try an unknown number of random elements, no duplicates ;)
     
  16. Offline

    CaptainDirt

    I already know Java, kthxbai
     
  17. Offline

    Cirno

    wat.
    You're telling someone to "First learn java" even if you can't even write proper and functioning example/spoonfeed code?
    This code doesn't even compile; how does one compare an object (HashSet) using a mathematical operation (<)?
    If you did fix it, as mentioned above, it has a chance to create an infinite loop.

    <rant>Your "simpler" way defeats its own description. Your analogy also makes no sense either; you want me to put coffee into a bowl, get "the" cup, and then put... more coffee in the bowl?</rant>
     
    ferrybig and CaptainDirt like this.
  18. Offline

    CaptainDirt

    Thanks all for the replies, problem is now fixed :)
     
  19. Offline

    FisheyLP

    why convert it to an arraylist?! example:
    Code:Java
    1.  
    2. //Array
    3. String[] myStrings = new String[]{"A", "B", "C", "D", "E"};
    4.  
    5. //Get the random with array[random];
    6. myStrings[new Random().nextInt(myStrings.length)];
    7.  
     
  20. Offline

    mrCookieSlime Retired Staff

    FisheyLP

     
  21. Offline

    FisheyLP

    Oh, I didn't read it before ._.
     
Thread Status:
Not open for further replies.

Share This Page