产生从百分比Java中的数组

问题描述 投票:1回答:4

我对这里的编码逻辑的问题。我不得不产生布尔数组,基于百分比。

为了澄清,我有一个百分比“X”(int值),我想生成布尔的阵列,其由1牛X百分数,随机分布的。此外,阵列的长度是恒定的。

例如,我想生成我的数组布尔的基础上,X = 40,我会:

[0,1,0,1,0,0,0,0,1,1,0,0,1,0,0,1,1,0,1,0]

我没有设法找到任何简单的解决方案或现有的功能来生成这个数组。梅斯有人可以帮我这个?

谢谢 :)

java arrays methods percentage
4个回答
1
投票

Random shuffling of an array介绍了如何洗牌数组。

// Create an array.  Initially elements are zero
int[] arr = new int[n];
// Put the right number of 1's in it
double limit = n * (X / 100.0);
for (int i = 0; i < limit; ++i) {  // Assumes X <= 100
  arr[i] = 1;
}
// Randomize the order of elements.
shuffleArr(arr);

1
投票

您可以使用(Math.random() < percentage)获得falsetrue与所需的概率。

double percentage = 0.4; // use 0.0 <= percentage <= 1.0
boolean[] array = new boolean[100];
for (int i = 0; i < array.length; i++) {
    array[i] = (Math.random() < percentage);
}

1
投票

这是给你的方法:

import java.util.Arrays;
import java.util.Collections;
import java.util.List;    

public class RandomizeArray {    

    public static void main(String args[]) {
        Boolean[] myArray = new Boolean[40];
        int xPercentage = 40;
        int ratio = myArray.length * xPercentage / 100;

        Arrays.fill(myArray, Boolean.FALSE);

        for(int i = 0; i<ratio; i++) {
            myArray[i] = true;
        }

        List<Boolean> l = Arrays.asList(myArray);
        Collections.shuffle(l);

        System.out.println(l);

    }    

}

输出:

[false, false, false, false, true, true, true, false, false, false, true, false, false, true, false, true, true, true, false, false, true, false, false, true, false, false, true, true, false, true, false, false, false, false, true, false, true, false, false, true]

[false, false, true, false, false, false, true, false, true, true, false, false, false, false, false, true, true, true, false, false, false, true, false, false, true, true, true, false, false, false, false, true, true, false, false, true, false, true, false, true]

0
投票

考虑一个效用函数,这是我看到用场的许多用例,如:

// Bases on {@link java.util.Random#ints(int, int)}, but "with uniqueness and limit".
static IntStream uniqueInts(int min, int max, int count, java.util.Random rnd) {
    // check parameters ... (max > min), (max - min > count), (rnd != null...)
    // call Random.ints(min, max) with distinct and limit
    return rnd.ints(min, max).distinct().limit(count);
}

,然后用位集合应用到你的情况......再次,因为我讨厌的事实“浪费每个boolean 7位”:

static BitSet randomBits(int total, int goodPercent, Random rand) {
    final BitSet bitSet = new BitSet(total);
    uniqueInts(0, total,  total * goodPercent / 100, rand)
        .forEach(i -> {
            bitSet.set(i);
        });
    // bitsSet.cardinality() == total * goodPercent / 100 (!)
    return bitSet;
}

..和最后(打印及主要方法):

static void printBits(int length, BitSet bs, PrintStream out) {
    int i = 0;
    out.append('[');
    for (; i < bs.length(); i++) {
        out.append(bs.get(i) ? '1' : '0');
    }
    // fill with zeros (according to BitSet.length() definition...
    // and provide parameter, since BitSet lacks this information).
    for (; i < length; i++) {
        out.append('0');
    }
    out.append(']');
    out.println();
}

public static void main(String[] args) {
    int total = 20;
    int goodPercent = 40;
    Random rand = new Random();
    // repeat it total times, to make a nice square
    for (int i = 0; i < total; i++) {
        BitSet test = randomBits(total, goodPercent, rand);
        printBits(total, test, System.out);
    }
}

输出:

[01100011011001010000]
[01100000000101011110]
[00000101101110001001]
[01001001000110100110]
[01001110100001110000]
[00100110011100000011]
[01011100001011001000]
[00000011101101100010]
[11101000110000010010]
[01010100100011011000]
[10000101100010001101]
[00100001110010110001]
[01100000010111100001]
[10000001110101000110]
[00001010011010100011]
[01101000001110100001]
[01000100110000101101]
[00110000001010011110]
[10011011100000000011]
[01011000010111000100]
© www.soinside.com 2019 - 2024. All rights reserved.