- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写这个程序,随机选择 3 个名称当选择一个名字时,每次弹出该名字时,该名字再次出现的概率就会降低 10%。
问题是,当我将 for 循环中的所有名称更改为一个名称时,我得到了我将所有内容更改为的名称的 90% 以及其他 2 个名称之一的 10%。
String [] arr = new String[30];
int i; int b; int g;
for (i = 0; i < 11; i++)
{
arr[i] = "moshe";
}
for (b = 9; b < 20; b++)
{
arr[b] = "Nir";
}
for (g = 22; g < 29; g++)
{
arr[g] = "Yoad";
}
double letsdomath = Math.random()*arr.length; // Exp: return the no. 10 / 30 letsdomath = 10
if (letsdomath < 11) // if i get moshe
{
for (i = 0; i <= letsdomath - 1; i++)
{
arr[i] = "Nir"; // Originally it would be Moshe here
arr[i + 1] = "Nir";
}
}
if (letsdomath > 11 && letsdomath < 21) // if i get nir
{
for (b = 0; b <= letsdomath -1; b++)
{
arr[b] = "Nir";
arr[b + 1] = "Nir"; // Originally it would be Yoad here
}
}
if (letsdomath > 21 && letsdomath < 30) // if i get yoad
{
for (g = 0; g <= letsdomath -1; g++)
{
arr[g] = "Nir"; // Originally it would be Yoad here
arr[29] = "Nir"; // Originally it would be Moshe here
}
}
System.out.println(arr[(int) letsdomath]);
预期结果:
让 Nir 这个名字每次都弹出
实际结果:
Nir 这个名字大约有 90% 的时间出现,而 Yoad 这个名字大约有 10% 的时间出现。
最佳答案
让我们重新审视您对想要做的事情的描述:为什么不将您所说的每件事都实现为“我们可以做的事情”,然后运行“我们喜欢的尽可能多的迭代”,只需按顺序运行这些迭代?
例如,让我们从基础知识开始:
import java.util.*;
import java.lang.Math;
public class Test {
String[] names;
int len;
double[] probabilities, thresholds;
public static void main(String[] args) {
new Test();
}
public Test() {
init();
int steps = 5;
for (int i=0; i<steps; i++) {
// do the thing!
}
}
public void init() {
// set up a probability distribution
names = new String[]{"name1", "name2", "name3"};
len = names.length;
thresholds = new double[len];
probabilities = new double[len];
for (int i=0; i<len; i++) {
probabilities[i] = 1./len;
}
}
}
然后,我们确保有一种方法可以查找概率阈值:如果概率为 [0.4, 0.3, 0.3],那么我们需要阈值 [0, 0.4, 0.7],以便我们可以轻松地处理以下事实:随机数 >= 0 但 < 0.4 应解析为索引 0,数字 >= 0.4 但 < 0.7 应解析为索引 1,等等:
// turns [0.3, 0.4, 0.3] into [0, 0.3, 0.7]
public void setThresholds() {
double tally = 0;
for (int i=0; i<len; i++){
thresholds[i] = tally;
tally += probabilities[i];
}
}
然后,让我们根据我们选择的名称索引定义重新平衡概率的函数:
// turns [0.4, 0.3, 0.3] with pos=0 into [0.36, 0.32, 0.32]
public void updateProbabilities(int namePos) {
double sprinkle = (probabilities[namePos] * 0.1) / (len - 1.);
probabilities[namePos] *= 0.9;
for (int i=0; i<len; i++) {
if (i == namePos) continue;
probabilities[i] += sprinkle;
}
}
正确,完成所有设置后,我们现在可以更新 public Test()
来运行一百万次更新传递,依靠这些函数按我们的预期工作(您应该这样做,当然,请验证):
public Test() {
init();
int steps = 5;
for (int i=0; i<steps; i++) {
setThresholds();
double randomValue = Math.random();
// find the associated name by finding the index of
// the threshold that is higher than our random value.
int namePos = findIndex(randomValue);
if (namePos == -1) {
namePos = names.length;
}
namePos--;
updateProbabilities(namePos);
// String name = names[namePos];
// System.out.println("step " + i + ": picked " + name + " (index " + namePos + ") based on " + randomValue);
// System.out.println("new probabilities: " + Arrays.toString(probabilities));
}
System.out.println("Final probabilities: " + Arrays.toString(probabilities));
}
public int findIndex(double randomValue) {
for (int i=0; i<len; i++) {
if (thresholds[i] > randomValue) return i;
}
return -1;
}
将这些中间控制台日志注释掉,因为您不想看到一千个中介......当然,除非您这样做。
关于java - 名称结果与概率不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57022267/
接下来是我的代码: with open("test.txt") as f_in: for line in f_in: for char in line:
我们有一个六面骰子,面编号为 1 到 6。随着 n 的增加,在第 n 卷中第一次看到 1 的概率降低。我想找到最小的卷数,使得这个概率小于某个给定的限制。 def probTest(limit):
我只是想知道为什么运行下面的代码时出现错误。我正在尝试使用 numpy 为基于文本的游戏计算概率。下面的代码不是游戏本身的代码。这仅用于测试目的和学习。感谢您提前的答复,请对我宽容一点。 from n
我目前正在创建一个与多个arduino板通信的服务器软件。由于硬件原因,我使用UDP协议(protocol)。我有一个非常简单的机制,在大多数情况下,当包裹丢失时,它会重新发送包裹。我现在有两个问题:
我想在 LinearLayout 上添加一个 fling Action 。为此,我使用了以下代码。 public class NewsActivity extends Activity { .
下面是其中一个 facebook 谜题:我无法理解如何进行此操作。 你有 C 个容器、B 个黑球和无限数量的白球。您希望以一种方式在容器之间分配球,即每个容器至少包含一个球,并且选择白球的概率大于或等
我有一个希伯来语文本,就像 "×گض¸×¨ض´×™×،ض°×کוض¹×ں",我想将它转换为可读的 unicode 希伯来语字符。 我试过这段代码: const string Str = "×گض¸×
我正在尝试使用 Random.nextDouble() 获取 1.0 和 10.0 之间的随机双数: double number = 1.0 + (10.0-1.0) * Random.nextDou
我目前已经为二进制类实现了概率(至少我这么认为)。现在我想扩展这种回归方法,并尝试将其用于波士顿数据集。不幸的是,我的算法似乎被卡住了,我当前运行的代码如下所示: from sklearn impor
我在 2D 空间中有一小组数据点(大约 10 个),每个数据点都有一个类别标签。我希望根据现有数据点标签对新数据点进行分类,并关联属于任何特定标签类别的“概率”。 基于最近邻的标签来标记新点是否合适(
我正在做我的第一个 tensorflow 项目。 我需要获得给定输入和预期序列的 ctc 概率(不是 ctc 损失)。 在 python 或 c++ 中是否有任何 api 或方法可以做到这一点? 我更
我正在尝试通过 assignment 1斯坦福 cs244n 类(class)。问题 1b 强烈建议对 Softmax 函数进行优化。我设法得到了N维向量的Softmax。我还得到了 MxN 维矩阵的
我有一个预测算法的想法,该算法可以根据所选项目先前出现的顺序准确预测随机值,并分析模式以提高准确性。 基本上是一种接受两个参数的算法,一个是一组可能的选择;另一个是这些数字的历史,分析该模式并预测序列
自 HOURS 以来,我一直在努力思考这个 TopCoder 问题,但无法找到一个完美的解决方案,并找到了下面给出的一个使用得非常漂亮的解决方案! 我想弄清楚这个解决方案如何适用于给定的问题?而我当初
我只知道如何生成随机 boolean 值(真/假)。默认概率为 50:50 但是我怎样才能用我自己的概率生成真假值呢?假设它以 40:60 或 20:80 等的概率返回 true... 最佳答案 一种
对于以下示例,我如何计算 julia 中的百分位数/概率值/尾部区域 Example : N(1100, 200) #Normally distributed with mean 1100 & st
我正在尝试修改标准 kNN 算法来获取属于某个类别的概率,而不仅仅是通常的分类。我还没有找到太多关于概率 kNN 的信息,但据我了解,它的工作原理与 kNN 类似,不同之处在于它计算给定半径内每个类的
我正在使用 PostgreSQL 为我所有数据中的变量对计算经验概率密度函数。我试图确定在计算 PDF 之前索引是否/何时更有效。我像这样运行 EXPLAIN CREATE INDEX, EXPLAI
有谁知道当查询有偏移时如何在 MySql 中请求“实时结果集”(例如:select * from table limit 10 offset 20;)。它正在经历类似 的错误 'invalid use
unsigned long long int first( int b , int c){ int h=b; //int k; for(int k=b-1;k>c;k--){ b=b*k;
我是一名优秀的程序员,十分优秀!