Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(6);
System.out.println(randomInt);
This wouldn't work, as 0 would be a possible value, and 6 wouldn't.This is very simple, at least show that you put some effort trying to write this program and we can help you along the way. You will want to create a new Random.
Here I wrote you a random integer generator took 3 seconds.
Just loop it 20 times and you are set. What do you need with JOption or BufferedReader? Isn't the simplest answer almost always the right one?
Code:Random randomGenerator = new Random(); int randomInt = randomGenerator.nextInt(6); System.out.println(randomInt);
private static int rand(int min, int max) {
return ((int)((Math.random() * (max - min + 1)) + min));
}
private static int rand() {
int i = rand.nextInt(7);
if(i != 0)
return i;
return rand();
}
This wouldn't work, as 0 would be a possible value, and 6 wouldn't.
Something along the lines of:
Would work.Code:private static int rand(int min, int max) { return ((int)((Math.random() * (max - min + 1)) + min)); }
EDIT: Or something like
In which rand is an instance of java.util.Random.Code:private static int rand() { int i = rand.nextInt(7); if(i != 0) return i; return rand(); }
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(6);
int rolledint = randomint + 1;
System.out.println(rolledint);
Yes, it would. I was overthinking it.Wouldn't this just work?
Code:Random randomGenerator = new Random(); int randomInt = randomGenerator.nextInt(6); int rolledint = randomint + 1; System.out.println(rolledint);
Random randomGenerator = new Random();
int rolls[] = new int[20];
for(int i =0; i<20; i++{
int randomInt = (randomGenerator.nextInt(6))+1;
rolls[i]=randomInt;
[COLOR="YellowGreen"]// this is if you want it stored in an array[/COLOR]
System.out.println(rolls[i]);
}
This would be your best solution :
----Code:Random randomGenerator = new Random(); int rolls[] = new int[20]; for(int i =0; i<20; i++{ int randomInt = (randomGenerator.nextInt(6))+1; rolls[i]=randomInt; [COLOR="YellowGreen"]// this is if you want it stored in an array[/COLOR] System.out.println(rolls[i]); }
I hope your problems are solved, you havent posted back yet..
public static int displayRandomInteger(int max) {
return (int)(java.lang.Math.random() * (max + 1));
}