How to print 5 random colors from an array of colors
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
|
show 1 more comment
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
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
|
show 1 more comment
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
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
java
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
|
show 1 more comment
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
|
show 1 more comment
3 Answers
3
active
oldest
votes
Just Replace
System.out.println(i);
with
System.out.println(solution[i]);
add a comment |
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.
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
add a comment |
Simply print solution[i]
instead of i
:
System.out.println(solution[i]);
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Just Replace
System.out.println(i);
with
System.out.println(solution[i]);
add a comment |
Just Replace
System.out.println(i);
with
System.out.println(solution[i]);
add a comment |
Just Replace
System.out.println(i);
with
System.out.println(solution[i]);
Just Replace
System.out.println(i);
with
System.out.println(solution[i]);
answered Nov 11 '18 at 20:18
apandey846
8441019
8441019
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Simply print solution[i]
instead of i
:
System.out.println(solution[i]);
add a comment |
Simply print solution[i]
instead of i
:
System.out.println(solution[i]);
add a comment |
Simply print solution[i]
instead of i
:
System.out.println(solution[i]);
Simply print solution[i]
instead of i
:
System.out.println(solution[i]);
answered Nov 11 '18 at 20:16
Rene Knop
1,3463622
1,3463622
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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