Friday, 9 August 2013

Random number in java has equal probability? [on hold]

Random number in java has equal probability? [on hold]

Using Math.random(); does number generated 0.0 to less than 1.0 has equal
probability?
If possible solve this question:
In the land of Puzzlevania, Aaron, Bob, and Charlie had an argument over
which one of them was the greatest puzzler of all time. To end the
argument once and for all, they agreed on a duel to the death. Aaron was a
poor shooter and only hit his target with a probability of 1>3. Bob was a
bit better and hit his target with a probability of 1>2. Charlie was an
expert marksman and never missed. A hit means a kill and the person hit
drops out of the duel.
To compensate for the inequities in their marksmanship skills, the three
decided that they would fire in turns, starting with Aaron, followed by
Bob, and then by Charlie. The cycle would repeat until there was one man
standing, and that man would be the Greatest Puzzler of All Time. An
obvious and reasonable strategy is for each man to shoot at the most
accurate shooter still alive, on the grounds that this shooter is the
deadliest and has the best chance of hitting back.
Write a program to simulate the duel using this strategy. Your program
should use random numbers and the probabilities given in the problem to
determine whether a shooter hits the target. Create a class named Duelist
that contains the dueler's name and shooting accuracy, a Boolean
indicating whether the dueler is still alive, and a method ShootAtTarget (
Duelist target ) that sets the target to dead if the dueler hits his
target (using a random number and the shooting accuracy) and does nothing
otherwise.
Once you can simulate a single duel, add a loop to your program that
simulates 10,000 duels. Count the number of times that each contestant
wins and print the probability of winning for each contestant (e.g., for
Aaron your program might output "Aaron won 3,595>10,000 duels or 35.95%").
MY TRY:
public class DuelistDemo {
public DuelistDemo() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
boolean check;
int x=1;
int bobWin=0, charlieWin=0, aaronWin=0;
Duelist one=new Duelist("Charlie","1");
Duelist two= new Duelist("Bob","1/2");
Duelist three= new Duelist("Aaron","1/3");
for(int i=1;i<=10000;i++){
check=three.shootAtTarget(one);
first:
while(check){
check=two.shootAtTarget(one);
if(check){
check=one.shootAtTarget(two);
if(check){
}
else{
// System.out.println("Charlie shot Bob");
check=three.shootAtTarget(one);
if(check){
check=one.shootAtTarget(three);
// System.out.println("Charlie shot Aaron, Charlie wins");
charlieWin++;
}
else
// System.out.println("Aaron shot Charlie, Aaron wins");
aaronWin++;
}
}
else{
// System.out.println("Bob shot Charlie");
check=three.shootAtTarget(two);
if(check){
check=false;
x=0;
break first;
}
else
// System.out.println("Aaron shot Bob");
// System.out.println("Charlie shot Aaron, Charlie wins");
charlieWin++;
}
check=true;
x=0;
break;
}
for(;x==1;){
// System.out.println("Aaron shot Charlie");
x=0;
}
while(check==false){
check=two.shootAtTarget(three);
if(check){
check=three.shootAtTarget(two);
if(check){
check=false;
continue;
}
else
// System.out.println("Aaron shot Bob, Aaron wins");
aaronWin++;
}
else
// System.out.println("Bob shot Aaron, Bob wins");
bobWin++;
break;
}
}
int total=charlieWin+bobWin+aaronWin;
System.out.println("Charlie wins:"+charlieWin+" Bob wins:"+bobWin+"
Aaron wins:"+aaronWin+" Total:" +total);
}
}
public class Duelist {
String name;
String accuracy;
boolean alive=true;
public Duelist() {
}
public Duelist(String name,String accuracy){
this.name=name;
this.accuracy=accuracy;
}
private boolean Duel(){
int ran=0;
if(accuracy=="1/3"){
ran=(int)(Math.random()*3)+ 1;
// System.out.println("ran:"+ran);
}
else if(accuracy=="1/2"){
ran=(int) (Math.random()*2)+1;
// System.out.println("ran:"+ran);
}
else
return false;
if(ran==1) return false;
else return true;
}
public boolean shootAtTarget(Duelist target){
target.alive= Duel();
// System.out.println(accuracy);
return target.alive;
}
}
RESULT
Charlie wins:3317 Bob wins:4165 Aaron wins:2518 Total:10000
Question says: Aaron wins 3,595>10000. Any help!

No comments:

Post a Comment