Java 随机生成数组中的-1或1序列

Java 随机生成数组中的-1或1序列,java,random,Java,Random,需要在for循环的每个步骤中打印自旋配置 对于n=4的配置,我们可以 1 1 1 1 1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 -1 -1 1 我需要随机生成1或-1,如果n=4有一个长度为4的数组,长度为1或-1,在这种情况下,最多可以有2^n个可能的配置。 需要打印出这些

需要在for循环的每个步骤中打印自旋配置 对于n=4的配置,我们可以

 1     1    1     1            
 1     1   -1     1           
-1     1   -1     1           
-1     1   -1     1            
-1    -1   -1     1  
我需要随机生成1或-1,如果n=4有一个长度为4的数组,长度为1或-1,在这种情况下,最多可以有2^n个可能的配置。 需要打印出这些可能的配置。不知道该怎么办?任何帮助都将不胜感激。多谢各位

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;
    }
}

这是通过检查每个2^n组合来实现的,查看组合中设置了哪些位。如果设置了位,则在数组中放入“1”,否则在数组中放入“-1”

import java.math.BigInteger;
import java.util.Arrays;

public class AllSeq {

    public static void main(String[] args) {
        // Get the number of elements from args or default to 4
        int n = args.length > 0 ? Integer.parseInt(args[0]) : 4;

        // Work out total number of combinations (2^n)
        BigInteger combinations = BigInteger.valueOf(2).pow(n);

        // For each combination...
        for(BigInteger i = BigInteger.ZERO; i.compareTo(combinations) < 0; i = i.add(BigInteger.ONE)) {
            // Initialise an array with 'n' elements
            int[] resultForThisCombination = new int[n];

            // We now go through each bit in the combination...
            for(int bit = 0; bit < n; bit++) {
                BigInteger bitValue = BigInteger.valueOf(2).pow(bit);

                // If the bit is set, set array element to 1 else set it to -1...
                if(i.and(bitValue).equals(bitValue)) {
                    resultForThisCombination[bit] = 1;
                } else {
                    resultForThisCombination[bit] = -1;
                }
            }

            // Print result / do whatever with it
            System.out.println(Arrays.toString(resultForThisCombination));
        }
    }
}
如果你为n输入一个大的数字,你可能会等待一段时间

如果n永远不超过63如果你想等那么久!!,可以使用long而不是BigInteger

class Solution {
    public static void main(String[] args) {
        List<List<Integer>> result = generateSequences(4);
        for(int i=0;i<result.size();++i){
            System.out.println(result.get(i).toString());
        }
    }

    private static List<List<Integer>> generateSequences(int seq_size){
        List<List<Integer>> result = new ArrayList<List<Integer>>();

        // for our recursion base case
        if(seq_size == 1){
            List<Integer> new_seq_1 = new ArrayList<>(); // add -1 once 
            new_seq_1.add(-1);
            List<Integer> new_seq_2 = new ArrayList<>(); // add 1 once
            new_seq_2.add(1);
            result.add(new_seq_1);
            result.add(new_seq_2);
            return result;
        }

        List<List<Integer>> sub_ans = generateSequences(seq_size - 1);

        for(int i=0;i<sub_ans.size();++i){
            List<Integer> new_seq_1 = new ArrayList<>(sub_ans.get(i)); // add -1 once 
            new_seq_1.add(-1);
            List<Integer> new_seq_2 = new ArrayList<>(sub_ans.get(i)); // add 1 once
            new_seq_2.add(1);
            result.add(new_seq_1);
            result.add(new_seq_2);
        }

        return result;
    }
}
算法:

我们可以递归地这样做。 所以,对于每个位置,我们可以有一个-1或1。既然你想要所有可能的序列,我们需要取-1和1。 因此,如果大小为1,则两种可能性分别为[-1]和[1]。 让我们通过一个示例来了解大小为2的序列的可能性

对于大小=1=>[-1],[1] 对于大小=2,将-1添加到所有先前的可能性中,并将1添加到先前的可能性中。因此,我们将 [-1,-1] [1, -1] [-1,1] [1,1] 同样,每个新的可能性取决于它的子集可能性,向每个可能性添加curr元素会生成当前的新可能性/序列

稍微改进一下,我们可以避免创建太多的大整数和执行位掩码操作:

导入java.math.biginger; 导入java.util.array; 公共类AllSeq{ 公共静态无效字符串[]args{ //从args或default中获取元素数为4 int n=args.length>0?整数。parseIntargs[0]:4; //计算出组合的总数2^n biginger bitValue=biginger.valueOf2.pown; int firstOne=n-1; //对于每个组合。。。 whilefirstOne>=0{ bitValue=bitValue.subtractBigInteger.ONE; firstOne=bitValue.getLowestSetBit //初始化数组,将“n”个元素全部设置为-1 int[]resultfortiscombination=新int[n]; Arrays.FillResultForIsCombination,-1; iffirstOne>=0{ //现在我们来看看组合中的每一位。。。 forint位=第一位;位不知道你想做什么。你想打印出所有可能的序列吗?例如,如果我输入3,我得到{-1,-1,-1}、{-1,-1,1}、{-1,1,-1}、{-1,1,1}、{1,-1,-1}、{1,-1,1}、{1,1,-1}、{1,1,1}?您的代码已经打印出四个随机的一或负一。你的问题是什么?是否要打印所有可能的组合?如果是这样,那就尽可能远离随机。不要继续创建新的随机实例。这样做并不随机。创建一个并重用它。因为您需要生成所有可能的序列,所以根本不存在随机性的问题。所有可能的序列都会考虑随机性。每个位置都有-1或1,这更像是一个算法问题。@vivek_23好的,如果每个位置都有-1或1,那么我可以先生成一个随机序列,然后改变每个位置。所以对于n=2,我会说随机生成的1,然后我可以把第一个位置改成-1,把第二个位置改成1,得到-1,然后试着修改-1,得到-1-1,但是这看起来很复杂,如果我想得到4个组合的和,我能得到这个forint位=0内的和吗;位[-1, -1, -1, -1] [-1, -1, -1, 1] [-1, -1, 1, -1] [-1, -1, 1, 1] [-1, 1, -1, -1] [-1, 1, -1, 1] [-1, 1, 1, -1] [-1, 1, 1, 1] [1, -1, -1, -1] [1, -1, -1, 1] [1, -1, 1, -1] [1, -1, 1, 1] [1, 1, -1, -1] [1, 1, -1, 1] [1, 1, 1, -1] [1, 1, 1, 1]