vendredi 20 mars 2020

Random numbers and their respective percentages

I found a task (in my book) that i decided to code, and it all works well and stuff...but it doesn't! or it does, i dont know. What i get as an answer is (look down):

Which seemed fine at first but then i wondered, why do the percentages (all together) not add up to 100%? or to 10000? (It amounts up to ~90%) Is this supposed to be a not full percentage? I was wondering why it does not give the full value back and did some research but couldnt find any satisfactory result.I wanna know: A)did i do a mistake in my coding? then what was it? B)Is it a normal output? then why and how it works?

Sorry if i got too tiresome but i'm quite intrigued by the output and why it is that way. (if you didnt understand what i'm looking for, feel free to ask)

ANSWER : /////////////////// *****Duel #1

Aaron won 2389.0/10000 duels or 23.89%

Bob won 4191.0/10000 duels or 41.91%

Charlie won 2275.0/10000 duels or 22.75%*****//////////////////////

import java.util.*;

public class Duelist{

private String name;
private double acc;
private boolean alive;

public Duelist(String newName, double newAcc)
{
    name = newName;
    acc = newAcc;
    alive = true;
}

public void stillAlive()
{
    alive = true;
}

public void dead()
{
    alive = false;
}

public boolean isAlive()
{
    return (alive == true);
}


public void shootAtTarger(Duelist target)           /** Generating new random # for each shot. Dead or Alive after, correspondingly */
{
    Random random = new Random();
    if (random.nextDouble() < acc)      
    {
        target.dead();
    }else {
        target.stillAlive();
    }
}

public static void main( String args[] ) {

    double aaronWins = 0;
    double bobWins = 0;
    double charlieWins = 0;

    Duelist aaron = new Duelist("Aaron", 0.33);
    Duelist bob = new Duelist("Bob", 0.5);
    Duelist charlie = new Duelist("Charlie", 1);

    System.out.println("Duel #1");

    for (int i = 0; i < 10000; i++)                 /** for 10000 fights*/ 
    {
        aaron.stillAlive();
        bob.stillAlive();
        charlie.stillAlive();           /** Setting them to alive at every start of a fight*/

        int peopleAlive = 3;
        while (peopleAlive > 1)     /**Fight till one is alive */
        {
            if (aaron.isAlive())        /** The system is: they need to hit everyone in this pattern/line up .. The lowest accuracy guy fires 1st and prioritizes the one with the highest accuracy */
            {                                           
                if (charlie.isAlive())                      
                {
                    aaron.shootAtTarger(charlie);
                    {
                        if (!(charlie.isAlive()))
                        {
                            peopleAlive--;
                        }else
                        {
                            charlie.stillAlive();
                        }
                    }
                }else
                {
                    if (bob.isAlive())
                    {
                        aaron.shootAtTarger(bob);

                        if (!(bob.isAlive()))
                        {
                            aaronWins++;                /** The other guy is dead, so he comes to the last one. If he dies +1 win. (Same with others) */
                            peopleAlive--;
                        }else
                        {
                            bob.stillAlive();
                        }
                    }
                }
            }
            if (bob.isAlive())
            {
                if (charlie.isAlive())
                {
                    bob.shootAtTarger(charlie);
                    {
                        if (!(charlie.isAlive()))
                        {
                            peopleAlive--;
                        }else
                        {
                            charlie.stillAlive();
                        }
                    }
                }else
                {
                    if (aaron.isAlive())
                    {
                        bob.shootAtTarger(aaron);

                        if (!(aaron.isAlive()))
                        {
                            bobWins++;
                            peopleAlive--;
                        }else
                        {
                            aaron.stillAlive();
                        }
                    }
                }
            } 
            if (charlie.isAlive())
            {
                if (bob.isAlive())
                {
                    charlie.shootAtTarger(bob);
                    {
                        if (!(bob.isAlive()))
                        {
                            peopleAlive--;
                        }else
                        {
                            bob.stillAlive();
                        }
                    }
                }else
                {
                    if (aaron.isAlive())
                    {
                        charlie.shootAtTarger(aaron);

                        if (!(aaron.isAlive()))
                        {
                            charlieWins++;
                            peopleAlive--;
                        }else
                        {
                            aaron.stillAlive();
                        }
                    }
                }
            } 
        }
    }
}

}




Aucun commentaire:

Enregistrer un commentaire