diff --git a/Cow.java b/Cow.java index cab68c9..ad6e2e9 100755 --- a/Cow.java +++ b/Cow.java @@ -38,10 +38,10 @@ name = makeName(sex, id); generation = 1; traits = new HashMap(); - traits.put("milk", 1); - traits.put("strength", 5); - traits.put("meat", 1); - traits.put("aggression", 10); + traits.put("milk", Herd.randomInt(5)); + traits.put("strength", Herd.randomInt(5)); + traits.put("meat", Herd.randomInt(5)); + traits.put("aggression", Herd.randomInt(10)); parents = new int[] {0,0}; } @@ -60,7 +60,7 @@ else generation = mother.getGeneration()+1; if (generation > maxGen) maxGen = generation; //Choose a random sex - if (Herd.random.nextInt(2) == 1) sex = "male"; + if (Herd.randomInt(2) == 1) sex = "male"; else sex = "female"; name = makeName(sex, id); //Calculate new trait values @@ -147,7 +147,7 @@ { int newtrait = (int) Math.round((fathertrait+mothertrait)/2); int quartertrait = (int) Math.round(newtrait*0.25); - int randomfactor = Herd.random.nextInt(quartertrait*2) - quartertrait; + int randomfactor = Herd.randomInt(quartertrait*2) - quartertrait; return newtrait+randomfactor; } diff --git a/Cow.java b/Cow.java index cab68c9..ad6e2e9 100755 --- a/Cow.java +++ b/Cow.java @@ -38,10 +38,10 @@ name = makeName(sex, id); generation = 1; traits = new HashMap(); - traits.put("milk", 1); - traits.put("strength", 5); - traits.put("meat", 1); - traits.put("aggression", 10); + traits.put("milk", Herd.randomInt(5)); + traits.put("strength", Herd.randomInt(5)); + traits.put("meat", Herd.randomInt(5)); + traits.put("aggression", Herd.randomInt(10)); parents = new int[] {0,0}; } @@ -60,7 +60,7 @@ else generation = mother.getGeneration()+1; if (generation > maxGen) maxGen = generation; //Choose a random sex - if (Herd.random.nextInt(2) == 1) sex = "male"; + if (Herd.randomInt(2) == 1) sex = "male"; else sex = "female"; name = makeName(sex, id); //Calculate new trait values @@ -147,7 +147,7 @@ { int newtrait = (int) Math.round((fathertrait+mothertrait)/2); int quartertrait = (int) Math.round(newtrait*0.25); - int randomfactor = Herd.random.nextInt(quartertrait*2) - quartertrait; + int randomfactor = Herd.randomInt(quartertrait*2) - quartertrait; return newtrait+randomfactor; } diff --git a/Herd.java b/Herd.java index c42b206..4fdb867 100755 --- a/Herd.java +++ b/Herd.java @@ -19,8 +19,7 @@ { public static int initialSize = 4; private static Herd singleton; - //allow other classes to access the RNG - public static Random random; + private static Random random; private ArrayList herd; private Cow[] parents; @@ -40,13 +39,16 @@ public static Herd getInstance() { - if (singleton == null) { - random = new Random(); - singleton = new Herd(); - } + if (singleton == null) singleton = new Herd(); return singleton; } + public static int randomInt(int max) + { + if (random == null) random = new Random(); + return random.nextInt(max); + } + public int size() { return herd.size();