jeudi 14 février 2019

randomly generate a sequence either -1 or 1 in an array

need to Print the spin configuration at each step in for loop So for n = 4 configuration we can have 1 1 1 1
1 1 -1 1
-1 1 -1 1
-1 1 -1 1
-1 -1 -1 1
I need to randomly generate 1 or -1 if n = 4 have an array of length 4 of 1 or -1 and you can have up to 2^n in this case 2^4 possible configuration. Need to print out these possible configuration. Not sure how to go about this? Any help would be appreciated. Thank you

import java.util.Random;

public class RandomTest {

    public static void main(String[] args) {
        for (int i = 0; i < 4; i++) {
            System.out.println(randomOneOrMinusOne());

        }
    }

    static int randomOneOrMinusOne() {
        Random rand = new Random();
        if (rand.nextBoolean())
            return 1;
        else
            return -1;
    }
}




Aucun commentaire:

Enregistrer un commentaire