-1

I am developing a game that gets the user to crack a vault code.

int [] vault = {1,2,3,4,5};

I currently just have per-determined values for the vault code (above) but I think it would be best if they changed with each play-through of the game.

I have seen the math.random method but I'm unsure how to make it only display integer values.

nsteeves
  • 31
  • 1
  • 6

2 Answers2

1

with java 8 you can use the random Object's method ints:

Random r = new Random();
long l = 10;
int lowBound =1;
int highBound =100;
int[] myArray = r.ints(l, lowBound, highBound).toArray();

System.out.println(Arrays.toString(myArray));
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You can do something like this:-

import java.util.Random;

Random rand = new Random();

for(int i=0;i<5;i++){
value[i] = rand.nextInt(5) + 1;  //5 will be maximum and 1 will be minimum
}

//value now has 5 ints generated randomnly.
Asim
  • 1,430
  • 1
  • 22
  • 43