I am creating a simple Mastermind game, where my colors are represented by numbers from 0 to 9. The program should generate a random code with length from 3 to 9 digits. I decided to use an array to hold my numbers 0 to 9, but I don't know how to generate a random number with a random length from this array. Can somebody help me please ?
Asked
Active
Viewed 2,382 times
0
-
You can see the answer to this question [here][1]. [1]:http://stackoverflow.com/questions/363681/generating-random-number-in-a-range-with-java – DigCamara Jan 23 '13 at 19:02
-
Is the generated code allowed to contain duplicate digits? For example, is 23389 valid? – Dan Dyer Jan 23 '13 at 19:44
4 Answers
1
Use the random number generator:
Random rnd=new Random()
int x=rnd.nextInt(10)

PearsonArtPhoto
- 38,970
- 17
- 111
- 142
-
Yup, i know that, but my explanation was a bit stupid. Excuse me for that. I want to have a combination from my array numbers. For example if the numbers are 0,1,2,3,4,5,6,7,8,9 I want a random combination with a random length to be generated. What you guys are giving me is a little bit diferrent. – Peter Georgiev Jan 23 '13 at 19:08
-
2
1
Here is the sample code:
package testing.Tests_SO;
import java.util.Arrays;
import java.util.Random;
public class App14487237 {
// creating random number generator object
public static final Random rnd = new Random();
public static int[] getNumber() {
// generating array length as rundom from 3 to 9 inclusive
int length = rnd.nextInt(7) + 3;
// creating an array
int[] number = new int[length];
// filling an array with random numbers from 0 to 9 inclusive
for(int i=0; i<number.length; ++i) {
number[i] = rnd.nextInt(10);
}
// returning and array
return number;
}
public static void main(String[] args) {
// generating number 10 times and prin result
for(int i=0; i<10; ++i) {
System.out.println( "attempt #" + i + ": " + Arrays.toString( getNumber() ) );
}
}
}
Here is it's output:
attempt #0: [0, 2, 6, 2, 5, 7, 5, 3]
attempt #1: [6, 2, 6, 6, 6, 2]
attempt #2: [8, 9, 6]
attempt #3: [6, 4, 7, 2, 1, 5, 7, 0]
attempt #4: [8, 2, 6, 7, 3, 8, 2, 9, 1]
attempt #5: [8, 6, 5, 9, 8, 8, 3, 9]
attempt #6: [6, 2, 3, 8, 6]
attempt #7: [3, 4, 6, 2]
attempt #8: [0, 5, 0, 0, 5, 8, 9, 4, 6]
attempt #9: [2, 2, 3, 3, 4, 9, 0]
P.S.
To print close:
int[] number;
for(int i=0; i<10; ++i) {
number = getNumber();
System.out.print( "attempt #" + i + ": " );
for(int j=0; j<number.length; ++j ) {
System.out.print(number[j]);
}
System.out.println();
}

Suzan Cioc
- 29,281
- 63
- 213
- 385
-
Fantastic. Thank you very very much. Just one more thing. I want to exclude the zero number from the array. How can I do that ? – Peter Georgiev Jan 24 '13 at 10:31
-
Then write `rnd.nextInt(9)+1` instead of `rnd.nextInt(10)` while generating a number. – Suzan Cioc Jan 24 '13 at 15:02
-
Okay, one last question. Can you please tell me how can I print the array as a String, without any commas. For example take attempt #2 : [8, 9, 6]. I want the otput to be: 896. Thanks in advance. I hope you answer me soon = ) – Peter Georgiev Jan 24 '13 at 18:25
0
Use a random number generator to determine the length of the string.
Then use a random number generator in a for loop to choose each digit from your array.
Something like this:
// the array of valid numbers
int[] numbers = // intialize this however you want
Random rand = new Random();
// determine random length between 3 and 9
int numLength = rand.nextInt(7) + 3;
StringBuilder output = new StringBuilder();
for (int i = 0; i < numLength; i++) {
// use a random index to choose a number from array
int thisNum = rand.nextInt(numbers.length);
// append random number from array to output
output.append(thisNum);
}
System.out.println(output.toString());

jahroy
- 22,322
- 9
- 59
- 108
0
I believe what you want is part of a random permutation of the elements in your array. This could be done in two steps.
Use a random generator to repeatedly swap elements in the array.
int n = arr.length; for (int i = 0; i < n-1; i++) { int j = i + rnd.nextInt(n-i); // select element between i and n // swap elements i and j in the array }
Select a random length
ln
and take the elements from0
toln

madth3
- 7,275
- 12
- 50
- 74