我这里有一个关于编码逻辑的问题。我必须根据百分比生成一个布尔数组。
为澄清起见,我得到一个百分比“ X”(int值),并且我想生成一个布尔数组,该布尔数组由x的1个百分数组成。而且,数组的长度是恒定的。
例如,我想基于X = 40生成布尔数组,我将:
[0,1,0,1,0,0,0,0,1,1,0,0,1,0,0,1,1,0,1,0]
我没有找到任何简单的解决方案或现有函数来生成此数组。有人可以帮我吗?
谢谢:)
答案 0 :(得分:1)
Random shuffling of an array说明了如何对数组进行混洗。
ref_dict = dict(zip(df['REFERENCE_CODE'], df['TRANSLATION']))
答案 1 :(得分:1)
您可以使用(Math.random() < percentage)
获取
false
或true
,并具有所需的概率。
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);
}
答案 2 :(得分:1)
这是为您提供的一种方法:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous">
</script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/af.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.10.0/fullcalendar.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.10.0/fullcalendar.min.css"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.10.0/fullcalendar.print.css"></script>
<script>
$(function() {
console.log($('#calendar'))
$('#calendar').fullCalendar({
defaultView: 'defaultView: "month"'
});
})
</script>
</head>
<body>
<!-- The calendar -->
<div id="calendar">
</div>
</body>
</html>
输出:
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);
}
}
答案 3 :(得分: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);
}
,然后再用BitSet应用于您的情况...,因为我讨厌这样的事实:“每个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]