0

I tried to write a program that would select teams to manage randomly,however when I run it I get the same 4 teams rather than different ones each time ?

I tried to make it so that each time I generate a random number it would go into an array.Then I would check against that array to see if the number had been used before.

Any help or advice would be appreciated. Thanks!

import java.util.*;

class Start {


    static String[] places = {"Man Utd", "Arsenal", "Aston Villa", "Chelsea", 
            "Everton", "Fulham", "Liverpool", "Man City", "Newcastle", "Norwich",
            "QPR", "Reading", "Southampton", "Stoke", "Sunderland", "Swansea", 
            "Spurs", "West Brom", "West ham", "Wigan"};

    static int[] NA = {21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21};
    static Random rand = new Random();
    static int RandInt = 0;
    static boolean x = false;
    static boolean p = false;
    static int player = 1;

    public static void main(String[] args) {

        while (x != true) {

            RandInt = rand.nextInt(places.length);
            for (int k = 0; k <= NA.length; k++) {
                while (p != true) {
                    if (RandInt == NA[k]) {
                        RandInt = rand.nextInt(places.length);
                    } else {
                        p = true;
                        NA[k] = RandInt;

                    }
                }
                System.out.println("player " + player + " is managing " + places[RandInt]);
                player++;
                p = false;
                if (player >= 5) {
                    x = true;
                    System.exit(0);
                }
            }
        }
    }
}
2
  • 1
    What is the point of int [] NA? Commented May 3, 2014 at 13:01
  • If you use an answer, please select it. Commented May 3, 2014 at 13:50

3 Answers 3

2

I "cleaned" your code a little bit and changed the Array to check duplicate random numbers to an ArrayList. It's not the fastest solution, but it should work.

The problem is that you don't get out of the for loop until the whole program exits. As geoand described above, RandInt == NA[k] will never be true, because RandInt will always be <= 19, so no new random number will be generated. So there are two wrong things in the code.

When you want to learn more about faster checking for duplicate entries, maybe this will help you: http://javarevisited.blogspot.de/2012/02/how-to-check-or-detect-duplicate.html

I hope I could help you. :)

static String[] places = {"Man Utd", "Arsenal", "Aston Villa", "Chelsea", 
        "Everton", "Fulham", "Liverpool", "Man City", "Newcastle", "Norwich",
        "QPR", "Reading", "Southampton", "Stoke", "Sunderland", "Swansea", 
        "Spurs", "West Brom", "West ham", "Wigan"};
static int[] NA = new ArrayList<Integer>(5);
static Random rand = new Random();
static int RandInt = 0;
static int player = 1;

public static void main(String[] args) {
  while (player < 5) {
    RandInt = rand.nextInt(places.length);

    for (int i = 0; i <= NA.size(); i++) {
      if (RandInt == NA.get(i)) {
        RandInt = rand.nextInt(places.length);
      } else {
        NA.add(RandInt);
        break;
      }
    }
    System.out.println("player " + player + " is managing " + places[RandInt]);
    player++;
  }
  System.exit(0);
}
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that RandInt==NA[k] is never true (because RandomInt is at most 19 due to the size of places) and therefore RandomInt is never updated inside the for loop. The while loop is only executed once since the for loop seems to do all the work

Is seems like you need to rethink your random generation algorithm

7 Comments

RandInt is updated every time the while loops iterates
Which is only once - check the flow of code and you will see that
No, it will iterate 4 times until player == 5 and player is incremented and tested in the main while loop
The while loop is controlled by the x flag which is only changed when player is incremented to 5. His for loops essentially does nothing because of what you said above, but the while loop is running 4 times. He even said he is getting the same 4 teams.
@NonSecwitter No problem, happens to everyone :)
|
0

My recommendation is to generate a random number inside the for loop as shown below

while (x != true) {

            for (int k = 0; k <= NA.length; k++) {
            RandInt = rand.nextInt(places.length);
                while (p != true) {
                    if (RandInt == NA[k]) {
                        RandInt = rand.nextInt(places.length);
                    } else {
                        p = true;
                        NA[k] = RandInt;

                    }
                }
                System.out.println("player " + player + " is managing " + places[RandInt]);
                player++;
                p = false;
                if (player >= 5) {
                    x = true;
                    System.exit(0);
                }
            }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.