2

I am trying to create a simple java program for a chess tournament, which will save game results in an array. Results will be stored according to users choice, they may be input from keyboard, or using results that are already in the array, OR generate random sequence of numbers 1.0, 0.0, 0.5. (win, loss, draw)

So far I know how to generate random numbers in a specific range, using java.util.Random;

public static void main(String[] args) {
        double a[][] = {{0.5, 0.5, 0.5, 0.5, 0.5},
                {0, 1, 0, 1, 1},
                {0.5, 1, 0.5, 0.5, 0},
                {0, 0.5, 0, 0.5, 0},
                {1, 1, 1, 1, 1},
                {0, 0, 0, 0.5, 0.5},
                {0, 0.5, 0, 0, 1}};
    int i, j;
    int ch;
    
    System.out.print("mode (1, 2 or 3): ");
    Scanner sc = new Scanner(System.in);
    ch = sc.nextInt();
    
    
            
    Random rnd = new Random();
    
    switch (ch) {
    case 1 -> { for (i=0; i<a.length ;i++) {
        for (j=0; j<a[i].length; j++) {
            a[i][j] = sc.nextDouble();
        }
    } 
    }

                
    case 2 -> { for (i=0; i<a.length; i++) {
        for (j=0; j<a[i].length; j++) {
            a[i][j] = rnd.nextDouble();
            }
        }
    }       
        
    case 3 -> { for (i=0; i<a.length; i++) {
        for (j=0; j<a[i].length; j++) {
            a[i][j] = a[i][j];
        }
    }
    }
    
    default -> {
        System.out.println("mode error");
        sc.close();
        return;
        }
    }
    
    sc.close();
    for (i=0; i<a.length; ++i) {
        for (j=0; j<a[i].length; ++j) {
            System.out.printf("%.1f" + " ", a[i][j]);
        }
        System.out.println();
    }

so case 2 of switch statement is giving me issues, since it gives an output of random numbers in a range of 0 to 1, but game results must be stored in values of 1.0, 0.5, and 0.0

3 Answers 3

2

You can make a new function in which you make your array for specific value then return a random value from that array and call that random function that you made in for loop like this:

   import java.util.Random;
   public static Double RandomGenerator() {
      Double[] arr = new Double[] { 0.0 , 0.5, 1.0 };
      Random rnd = new Random();
      Double result = arr[rnd.nextInt(arr.length)];
      return result;
   }
}

Then in your for loop you can do this:

 { for (i=0; i<a.length; i++) {
        for (j=0; j<a[i].length; j++) {
            a[i][j] = RandomGenerator();
            }

Which will return a double random value in your array.

Sign up to request clarification or add additional context in comments.

8 Comments

This didn't even compile for me. Perhaps you meant Double[] arr = new Double[] { 0.0 , 0.5, 1.0 }; and Double result = arr[(int)new Random().nextDouble(Double.valueOf(arr.length))];?
This idea is good, but yo souldn't instanciate a new array and a new Random object for each cell (because of each call), these should be global stuff
@JustAnotherDeveloper that was just a rough idea the code I just written you can compile this and it will work correctly
@Levi: Incorrect. The code as it is now still will not compile.
@Levi Again, literally impossible, as there is a closing parenthesis that is not opened (and then other issues with the array once you fix that). Here, take a look at this jdoodle and see for yourself that it shows "error: ';' expected" when you try to run it: jdoodle.com/ia/Aqh
|
1

Use nextInt(bound) with a bound of 3, to generate 0, 1, 2, then divide by 2

case 2 -> {
    for (i = 0; i < a.length; i++) {
        for (j = 0; j < a[i].length; j++) {
            a[i][j] = rnd.nextInt(3) / 2.0;
        }
    }
}

Note that case 3 is useless, it does nothing, the following is enough

case 3 -> System.out.println("Nothing to do");

Comments

0

You can have your possible valid values in an array:

float[] valuesArray = {1.0,0.0,0.5};

And then use something like the following to generate a valid index of the array that you can use to access one of your valid values:

public int getRandomNumber(int min, int max) {
    return (int) ((Math.random() * (max - min)) + min);
}

So if you call it like int myIndex = getRandomNumber(0, 2); and use its return value to access values from valuesArray you should be able to get only 0.0, 0.5 and 1.0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.