How to print 5 random colors from an array of colors










-1














I am trying to print 5 random colors (colors can be repeated as long as they are random) from an array of 10 colors that I created. I know I'm close-ish, but I'm not sure where I'm going wrong.



import java.util.Random;

public class RandomColors

public static void main (String args)

Random r = new Random();

String colors = "red","green","blue","yellow","brown","black","white","indigo","orange","purple";
String solution = new String[5];

for(int i = 0; i < solution.length; i++)

solution[i] = colors[r.nextInt(10)];
System.out.println(i);






I know my problem is that I'm trying to print "i", but I'm not sure what I should be combining to print.










share|improve this question























  • I.ToString()???
    – AndrewE
    Nov 11 '18 at 20:11






  • 2




    Replace System.out.println(i); with System.out.println(solution[i]);
    – apandey846
    Nov 11 '18 at 20:12











  • Thank you @apandey846, that was what I needed.
    – Squanching
    Nov 11 '18 at 20:15






  • 1




    Note that even with the fixes suggested here, you're not going to get different colours.
    – Boris the Spider
    Nov 11 '18 at 20:21










  • How can they be 5 different colours if they can be repeated? I'm not sure I understand the question...
    – Boris the Spider
    Nov 11 '18 at 20:33















-1














I am trying to print 5 random colors (colors can be repeated as long as they are random) from an array of 10 colors that I created. I know I'm close-ish, but I'm not sure where I'm going wrong.



import java.util.Random;

public class RandomColors

public static void main (String args)

Random r = new Random();

String colors = "red","green","blue","yellow","brown","black","white","indigo","orange","purple";
String solution = new String[5];

for(int i = 0; i < solution.length; i++)

solution[i] = colors[r.nextInt(10)];
System.out.println(i);






I know my problem is that I'm trying to print "i", but I'm not sure what I should be combining to print.










share|improve this question























  • I.ToString()???
    – AndrewE
    Nov 11 '18 at 20:11






  • 2




    Replace System.out.println(i); with System.out.println(solution[i]);
    – apandey846
    Nov 11 '18 at 20:12











  • Thank you @apandey846, that was what I needed.
    – Squanching
    Nov 11 '18 at 20:15






  • 1




    Note that even with the fixes suggested here, you're not going to get different colours.
    – Boris the Spider
    Nov 11 '18 at 20:21










  • How can they be 5 different colours if they can be repeated? I'm not sure I understand the question...
    – Boris the Spider
    Nov 11 '18 at 20:33













-1












-1








-1







I am trying to print 5 random colors (colors can be repeated as long as they are random) from an array of 10 colors that I created. I know I'm close-ish, but I'm not sure where I'm going wrong.



import java.util.Random;

public class RandomColors

public static void main (String args)

Random r = new Random();

String colors = "red","green","blue","yellow","brown","black","white","indigo","orange","purple";
String solution = new String[5];

for(int i = 0; i < solution.length; i++)

solution[i] = colors[r.nextInt(10)];
System.out.println(i);






I know my problem is that I'm trying to print "i", but I'm not sure what I should be combining to print.










share|improve this question















I am trying to print 5 random colors (colors can be repeated as long as they are random) from an array of 10 colors that I created. I know I'm close-ish, but I'm not sure where I'm going wrong.



import java.util.Random;

public class RandomColors

public static void main (String args)

Random r = new Random();

String colors = "red","green","blue","yellow","brown","black","white","indigo","orange","purple";
String solution = new String[5];

for(int i = 0; i < solution.length; i++)

solution[i] = colors[r.nextInt(10)];
System.out.println(i);






I know my problem is that I'm trying to print "i", but I'm not sure what I should be combining to print.







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 '18 at 20:44

























asked Nov 11 '18 at 20:07









Squanching

205




205











  • I.ToString()???
    – AndrewE
    Nov 11 '18 at 20:11






  • 2




    Replace System.out.println(i); with System.out.println(solution[i]);
    – apandey846
    Nov 11 '18 at 20:12











  • Thank you @apandey846, that was what I needed.
    – Squanching
    Nov 11 '18 at 20:15






  • 1




    Note that even with the fixes suggested here, you're not going to get different colours.
    – Boris the Spider
    Nov 11 '18 at 20:21










  • How can they be 5 different colours if they can be repeated? I'm not sure I understand the question...
    – Boris the Spider
    Nov 11 '18 at 20:33
















  • I.ToString()???
    – AndrewE
    Nov 11 '18 at 20:11






  • 2




    Replace System.out.println(i); with System.out.println(solution[i]);
    – apandey846
    Nov 11 '18 at 20:12











  • Thank you @apandey846, that was what I needed.
    – Squanching
    Nov 11 '18 at 20:15






  • 1




    Note that even with the fixes suggested here, you're not going to get different colours.
    – Boris the Spider
    Nov 11 '18 at 20:21










  • How can they be 5 different colours if they can be repeated? I'm not sure I understand the question...
    – Boris the Spider
    Nov 11 '18 at 20:33















I.ToString()???
– AndrewE
Nov 11 '18 at 20:11




I.ToString()???
– AndrewE
Nov 11 '18 at 20:11




2




2




Replace System.out.println(i); with System.out.println(solution[i]);
– apandey846
Nov 11 '18 at 20:12





Replace System.out.println(i); with System.out.println(solution[i]);
– apandey846
Nov 11 '18 at 20:12













Thank you @apandey846, that was what I needed.
– Squanching
Nov 11 '18 at 20:15




Thank you @apandey846, that was what I needed.
– Squanching
Nov 11 '18 at 20:15




1




1




Note that even with the fixes suggested here, you're not going to get different colours.
– Boris the Spider
Nov 11 '18 at 20:21




Note that even with the fixes suggested here, you're not going to get different colours.
– Boris the Spider
Nov 11 '18 at 20:21












How can they be 5 different colours if they can be repeated? I'm not sure I understand the question...
– Boris the Spider
Nov 11 '18 at 20:33




How can they be 5 different colours if they can be repeated? I'm not sure I understand the question...
– Boris the Spider
Nov 11 '18 at 20:33












3 Answers
3






active

oldest

votes


















1














Just Replace




System.out.println(i);




with




System.out.println(solution[i]);







share|improve this answer




























    5














    You have a problem that your algorithm might return the same color several times (r.nextInt(10) can return the same value multiple times).



    A more straightforward solution could be to convert the array to a list, shuffle it (this is the "random" part), and print the first 5 elements:



    List<String> colorsList = Arrays.asList(colors);
    Collections.shuffle(colorsList);
    colorsList.stream().limit(5).forEach(System.out::println);



    Since the question was edited to say that non-unique colors are allowed, they can be printed like this:



    IntStream.generate(() -> r.nextInt(colors.length))
    .limit(5)
    .mapToObj(i -> colors[i])
    .forEach(System.out::println);


    This generates a stream of 5 random integers, and prints the elements by accessing the array.






    share|improve this answer


















    • 1




      This is the only correct answer here.
      – Boris the Spider
      Nov 11 '18 at 20:26










    • It's okay if it returns the same color multiple times, as long as the results are random. I do see what you are saying though. Thank you.
      – Squanching
      Nov 11 '18 at 20:28






    • 3




      Well, your question says "5 different random colors".
      – Magnilex
      Nov 11 '18 at 20:29










    • I realized that after you answered my question. I edited my post. Sorry about that.
      – Squanching
      Nov 11 '18 at 20:30


















    2














    Simply print solution[i] instead of i:



    System.out.println(solution[i]);





    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252738%2fhow-to-print-5-random-colors-from-an-array-of-colors%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Just Replace




      System.out.println(i);




      with




      System.out.println(solution[i]);







      share|improve this answer

























        1














        Just Replace




        System.out.println(i);




        with




        System.out.println(solution[i]);







        share|improve this answer























          1












          1








          1






          Just Replace




          System.out.println(i);




          with




          System.out.println(solution[i]);







          share|improve this answer












          Just Replace




          System.out.println(i);




          with




          System.out.println(solution[i]);








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 '18 at 20:18









          apandey846

          8441019




          8441019























              5














              You have a problem that your algorithm might return the same color several times (r.nextInt(10) can return the same value multiple times).



              A more straightforward solution could be to convert the array to a list, shuffle it (this is the "random" part), and print the first 5 elements:



              List<String> colorsList = Arrays.asList(colors);
              Collections.shuffle(colorsList);
              colorsList.stream().limit(5).forEach(System.out::println);



              Since the question was edited to say that non-unique colors are allowed, they can be printed like this:



              IntStream.generate(() -> r.nextInt(colors.length))
              .limit(5)
              .mapToObj(i -> colors[i])
              .forEach(System.out::println);


              This generates a stream of 5 random integers, and prints the elements by accessing the array.






              share|improve this answer


















              • 1




                This is the only correct answer here.
                – Boris the Spider
                Nov 11 '18 at 20:26










              • It's okay if it returns the same color multiple times, as long as the results are random. I do see what you are saying though. Thank you.
                – Squanching
                Nov 11 '18 at 20:28






              • 3




                Well, your question says "5 different random colors".
                – Magnilex
                Nov 11 '18 at 20:29










              • I realized that after you answered my question. I edited my post. Sorry about that.
                – Squanching
                Nov 11 '18 at 20:30















              5














              You have a problem that your algorithm might return the same color several times (r.nextInt(10) can return the same value multiple times).



              A more straightforward solution could be to convert the array to a list, shuffle it (this is the "random" part), and print the first 5 elements:



              List<String> colorsList = Arrays.asList(colors);
              Collections.shuffle(colorsList);
              colorsList.stream().limit(5).forEach(System.out::println);



              Since the question was edited to say that non-unique colors are allowed, they can be printed like this:



              IntStream.generate(() -> r.nextInt(colors.length))
              .limit(5)
              .mapToObj(i -> colors[i])
              .forEach(System.out::println);


              This generates a stream of 5 random integers, and prints the elements by accessing the array.






              share|improve this answer


















              • 1




                This is the only correct answer here.
                – Boris the Spider
                Nov 11 '18 at 20:26










              • It's okay if it returns the same color multiple times, as long as the results are random. I do see what you are saying though. Thank you.
                – Squanching
                Nov 11 '18 at 20:28






              • 3




                Well, your question says "5 different random colors".
                – Magnilex
                Nov 11 '18 at 20:29










              • I realized that after you answered my question. I edited my post. Sorry about that.
                – Squanching
                Nov 11 '18 at 20:30













              5












              5








              5






              You have a problem that your algorithm might return the same color several times (r.nextInt(10) can return the same value multiple times).



              A more straightforward solution could be to convert the array to a list, shuffle it (this is the "random" part), and print the first 5 elements:



              List<String> colorsList = Arrays.asList(colors);
              Collections.shuffle(colorsList);
              colorsList.stream().limit(5).forEach(System.out::println);



              Since the question was edited to say that non-unique colors are allowed, they can be printed like this:



              IntStream.generate(() -> r.nextInt(colors.length))
              .limit(5)
              .mapToObj(i -> colors[i])
              .forEach(System.out::println);


              This generates a stream of 5 random integers, and prints the elements by accessing the array.






              share|improve this answer














              You have a problem that your algorithm might return the same color several times (r.nextInt(10) can return the same value multiple times).



              A more straightforward solution could be to convert the array to a list, shuffle it (this is the "random" part), and print the first 5 elements:



              List<String> colorsList = Arrays.asList(colors);
              Collections.shuffle(colorsList);
              colorsList.stream().limit(5).forEach(System.out::println);



              Since the question was edited to say that non-unique colors are allowed, they can be printed like this:



              IntStream.generate(() -> r.nextInt(colors.length))
              .limit(5)
              .mapToObj(i -> colors[i])
              .forEach(System.out::println);


              This generates a stream of 5 random integers, and prints the elements by accessing the array.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 11 '18 at 20:36

























              answered Nov 11 '18 at 20:22









              Magnilex

              7,03763459




              7,03763459







              • 1




                This is the only correct answer here.
                – Boris the Spider
                Nov 11 '18 at 20:26










              • It's okay if it returns the same color multiple times, as long as the results are random. I do see what you are saying though. Thank you.
                – Squanching
                Nov 11 '18 at 20:28






              • 3




                Well, your question says "5 different random colors".
                – Magnilex
                Nov 11 '18 at 20:29










              • I realized that after you answered my question. I edited my post. Sorry about that.
                – Squanching
                Nov 11 '18 at 20:30












              • 1




                This is the only correct answer here.
                – Boris the Spider
                Nov 11 '18 at 20:26










              • It's okay if it returns the same color multiple times, as long as the results are random. I do see what you are saying though. Thank you.
                – Squanching
                Nov 11 '18 at 20:28






              • 3




                Well, your question says "5 different random colors".
                – Magnilex
                Nov 11 '18 at 20:29










              • I realized that after you answered my question. I edited my post. Sorry about that.
                – Squanching
                Nov 11 '18 at 20:30







              1




              1




              This is the only correct answer here.
              – Boris the Spider
              Nov 11 '18 at 20:26




              This is the only correct answer here.
              – Boris the Spider
              Nov 11 '18 at 20:26












              It's okay if it returns the same color multiple times, as long as the results are random. I do see what you are saying though. Thank you.
              – Squanching
              Nov 11 '18 at 20:28




              It's okay if it returns the same color multiple times, as long as the results are random. I do see what you are saying though. Thank you.
              – Squanching
              Nov 11 '18 at 20:28




              3




              3




              Well, your question says "5 different random colors".
              – Magnilex
              Nov 11 '18 at 20:29




              Well, your question says "5 different random colors".
              – Magnilex
              Nov 11 '18 at 20:29












              I realized that after you answered my question. I edited my post. Sorry about that.
              – Squanching
              Nov 11 '18 at 20:30




              I realized that after you answered my question. I edited my post. Sorry about that.
              – Squanching
              Nov 11 '18 at 20:30











              2














              Simply print solution[i] instead of i:



              System.out.println(solution[i]);





              share|improve this answer

























                2














                Simply print solution[i] instead of i:



                System.out.println(solution[i]);





                share|improve this answer























                  2












                  2








                  2






                  Simply print solution[i] instead of i:



                  System.out.println(solution[i]);





                  share|improve this answer












                  Simply print solution[i] instead of i:



                  System.out.println(solution[i]);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 '18 at 20:16









                  Rene Knop

                  1,3463622




                  1,3463622



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252738%2fhow-to-print-5-random-colors-from-an-array-of-colors%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo