How can i improve this code so the random numbers don't repeat it self over and over?
int sum = -40, num = 40;
Random random = new Random();
for(int s = 27; s < 5; s++){
sum = 27 + random.nextInt(40);
System.out.println(sum + " ");
for(int n = 17; n > 17; n++){
num = 39 + random.nextInt(40);
System.out.println(num + " ");
}
}
if(num > 90){
System.out.println(" ");
num = 90;
}
else{
System.out.println(" ");
sum = 40 + sum + num * random.nextInt(40);
}
System.out.println(num + sum);
}
I have been working on this code and the code seems to be working fine but I want to achieve is to not to let the machine to repeat for example 240 like twice or any other is there any way this code can be improved by basic programming... Thanks
Comment
Answer by Commoble · Feb 13, 2018 at 03:50 AM
1) Put the numbers 0 through 39 (or whatever numbers you end up using) into a List or some other data structure
2) When you need a number, remove a random number from the list and use that
Answer by melsy · Feb 13, 2018 at 04:09 AM
use a list and add the random number that is called and do a check if the random number has been called by checking it against the list . could do this so a sequence of numbers as large as you want dont repeat