0
 public class Clan {
    private String clanName;
    static ArrayList<Citizen> list;
    private int clanSize;

  public Clan(String clanName, int clanSize) {
     this.clanName = clanName;
     this.clanSize = clanSize;
     Clan.list = new ArrayList<Citizen>(clanSize);

   //list.add(new Archer());
   for (int i = 0; i < clanSize; i++) {
       Clan.list.add(Citizen.randomCitizen());
   }

}

    public static Citizen randomCitizen() {
    Random randomReturn = new Random();

    Citizen [] list = new Citizen[3];

    list[0]=new Archer();
    list[1]= new Barbarian();
    list[2]= new Civilian();

    return list[randomReturn.nextInt(list.length)];
 
}

This is meant to randomly fill the clan with random citizens whenever its called. The error I am having is if I create a new clan(toronto, 5) and another new clan(montreal, 6). They both have the same pattern of citizens. Its meant to be random for each call

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
RAVE CLO
  • 14
  • 3
  • 1
    Don't you think you should include randomCitizen implementation? Also, static class variables are shared resources. That means each instance will have access to the same variable. – sagi Nov 17 '20 at 16:50
  • You are using static keyword with clan which makes it to share between object instances. – Asad Jivani Nov 17 '20 at 16:52
  • You are using static fields to save the List of Citizen. So all your clans will share one value for this static field. – OH GOD SPIDERS Nov 17 '20 at 16:52
  • 1
    Please try to make your question's title _specific to that individual question_. The original, pre-edit title could be about anything -- and in consequence, that means other people are unlikely to be able to find the question and learn from it, even if they have the same problem. – Charles Duffy Nov 17 '20 at 16:56
  • Another problem is creating many `Random` instances in a tight loop. These instances will share the same seed, thus generating the same random numbers. – knittl Nov 17 '20 at 17:02

0 Answers0