Am designing wumpus hunting program using java. Where i can include three gold, 3 pits, one wumpus, one player. and the position of the game item should be random generator.How to generate random characters in 2d array . How to specify the range for the characters in java. Thanks.
Asked
Active
Viewed 525 times
-5
-
1The best way to do it is to go and write some code – Tilak Madichetti May 22 '16 at 15:13
-
https://docs.oracle.com/javase/8/docs/api/java/util/Random.html – Tilak Madichetti May 22 '16 at 15:15
-
1Show me your efforts ! – Tilak Madichetti May 22 '16 at 15:16
-
This is my code so far.. i want exactly 1 wumpus, 1 player, 3 gold, 3 pit, others are clear ground in the 4*4 grid – user6367798 May 22 '16 at 15:28
-
Edit your original question. We need to see you trying to solve the problem – Arman May 22 '16 at 15:42
1 Answers
0
You can use the method random.nextInt()
to generate a random number for the position.
If you want to generate a random character you should use :
(char)(random.nextInt(max-min)+min)
. Where the max and min value are the ascii code of the max and the min char.
If you only want to generate a char in some char you should create an array with these character and use yourArray[random.nextInt(yourArray.length-1)+1];
Edit : You have to create the object random at the beginning of your code. Random random = new Random();

Dinidu Hewage
- 2,169
- 6
- 40
- 51