nvibest
09-17-2008, 09:48 PM
Hello,
I recently have been trying to write a simple program that involves the use of arrays and a random number generator in order to simulate rolling a six sided fair die.
I've posted my coding so far below...
import java.util.Random;
public class RollDie {
public static void main(String[] args) {
//Create an array of ints to hold the results of 5 rolls of a
//die.
int [] array = {1, 2, 3, 4, 5, 6};
//Create a Random object to generate random values
Random generator = new Random ();
//Use the Random object to place a random value between 1 and 6
//in each position of the array to hold the result of each roll
array[1] = generator.nextInt(6) + 1;
array[2] = generator.nextInt(6) + 1;
array[3] = generator.nextInt(6) + 1;
array[4] = generator.nextInt(6) + 1;
array[5] = generator.nextInt(6) + 1;
//Display the results of the five rolls
System.out.println ("Five rolls of a die: " + (array[1]) + " " + (array[2]) + " " + (array[3]) + " " + (array[4]) + " " + (array[5]) );
//Use the Random object to randomly select two of the five rolls
//(sometimes, one die may be selected twice-that's OK)
System.out.println ("Die 1 (from roll " + "2): "+ 5);
//Display the rolls selected and their sum as illustrated below.
}
}
I've been able to write it successfully up until the part where I need to take the 5, just rolled die, and randomly pick 2 of the roles to display in the format above.....e.i.' System.out.println ("Die 1 (insert first randomly picked die), //then on the second line.."Die 2 (insert second randomly picked die))....
As you can see I'm having some trouble figuring out how to randomly pick 2 out of the 5 arrays I "rolled" in "System.out.println ("Five rolls of a die: " + (array[1]) + " " + (array[2]) + " " + (array[3]) + " " + (array[4]) + " " + (array[5]) );
"
Any help would be greatly appreciated.
Thanks,
MC
I recently have been trying to write a simple program that involves the use of arrays and a random number generator in order to simulate rolling a six sided fair die.
I've posted my coding so far below...
import java.util.Random;
public class RollDie {
public static void main(String[] args) {
//Create an array of ints to hold the results of 5 rolls of a
//die.
int [] array = {1, 2, 3, 4, 5, 6};
//Create a Random object to generate random values
Random generator = new Random ();
//Use the Random object to place a random value between 1 and 6
//in each position of the array to hold the result of each roll
array[1] = generator.nextInt(6) + 1;
array[2] = generator.nextInt(6) + 1;
array[3] = generator.nextInt(6) + 1;
array[4] = generator.nextInt(6) + 1;
array[5] = generator.nextInt(6) + 1;
//Display the results of the five rolls
System.out.println ("Five rolls of a die: " + (array[1]) + " " + (array[2]) + " " + (array[3]) + " " + (array[4]) + " " + (array[5]) );
//Use the Random object to randomly select two of the five rolls
//(sometimes, one die may be selected twice-that's OK)
System.out.println ("Die 1 (from roll " + "2): "+ 5);
//Display the rolls selected and their sum as illustrated below.
}
}
I've been able to write it successfully up until the part where I need to take the 5, just rolled die, and randomly pick 2 of the roles to display in the format above.....e.i.' System.out.println ("Die 1 (insert first randomly picked die), //then on the second line.."Die 2 (insert second randomly picked die))....
As you can see I'm having some trouble figuring out how to randomly pick 2 out of the 5 arrays I "rolled" in "System.out.println ("Five rolls of a die: " + (array[1]) + " " + (array[2]) + " " + (array[3]) + " " + (array[4]) + " " + (array[5]) );
"
Any help would be greatly appreciated.
Thanks,
MC