英文:
Stuck on Discrete distribution having an issue with finishing the problem
问题
这是我应该得到的内容。
这是我实际得到的内容。
编写一个名为 DiscreteDistribution.java 的程序,它接受一个整数命令行参数 m,后面跟着一系列正整数命令行参数 a1,a2,…,an,然后打印 m 个随机索引(用空格分隔),每个索引 i 的选择概率与 ai 成比例。
到目前为止,我已经有了
public static void main(String[] args) {
// 随机索引的数量
int m = Integer.parseInt(args[0]);
// 读取 n 个值的出现频率
int n = args.length;
int[] freq = new int[n];
for (int i = 0; i < n; i++) {
freq[i] = Integer.parseInt(args[i]);
}
// 计算所有频率的总计数
int total = 0;
for (int i = 0; i < n; i++) {
total += freq[i];
}
for (int j = 0; j < m; j++) {
// 生成与频率成比例的随机整数
int r = (int) ((total) * Math.random() - 1); // 在 [0, total) 范围内的整数
int sum = 0;
int event = -1;
for (int i = 0; i < n && sum <= r; i++) {
sum += freq[i];
event = i;
System.out.println(freq[i]);
}
}
}
英文:
Here is what I am suppose to be getting.
This is what I am actually getting.
Write a program DiscreteDistribution.java that takes an integer command-line argument m, followed by a sequence of positive integer command-line arguments a1,a2,…,an, and prints m random indices (separated by whitespace), choosing each index i with probability proportional to ai.
So far I have
public static void main(String[] args) {
// number of random indices
int m = Integer.parseInt(args[0]);
// read in frequency of occurrence of n values
int n = args.length;
int[] freq = new int[n];
for (int i = 0; i < n; i++) {
freq[i] = Integer.parseInt(args[i]);
}
// compute total count of all frequencies
int total = 0;
for (int i = 0; i < n; i++) {
total += freq[i];
}
for (int j = 0; j < m; j++) {
// generate random integer with probability proportional to frequency
int r = (int) ((total) * Math.random() - 1); // integer in [0, total)
int sum = 0;
int event = -1;
for (int i = 0; i < n && sum <= r; i++) {
sum += freq[i];
event = i;
System.out.println(freq[i]);
}
}
}
答案1
得分: 1
根据我正确理解您的问题的假设,您可以使用以下算法根据给定的频率在范围从1到n
内生成m
个随机数:
public static void main(String[] args) {
// 随机数索引的数量
int m = Integer.parseInt(args[0]);
// 读取n个值的出现频率
int n = args.length;
int[] freq = new int[n];
for (int i = 1; i < n; i++) {
freq[i] = Integer.parseInt(args[i]);
}
// 计算所有频率的总计数
int total = 0;
for (int i = 1; i < n; i++) {
total += freq[i];
}
double[] summedProbabilities = new double[n];
for (int i = 1; i < summedProbabilities.length; i++) {
final double probability = freq[i] / (double) total;
summedProbabilities[i] = summedProbabilities[i - 1] + probability;
}
for (int j = 0; j < m; j++) {
// 生成具有与频率成比例的概率的随机整数
double randomProbability = Math.random();
int i = 1;
while (randomProbability > summedProbabilities[i]) {
i++;
}
System.out.print(i + " ");
if (j % 10 == 0) {
System.out.println();
}
}
}
我强烈建议您以一种使用方法来计算小块并将其组合在一起的方式重构代码。
英文:
Under the assumption that I understand your problem correctly, then you can use the following algorithm to produce m
random numbers in the range 1 to n
according to the given frequencies:
public static void main(String[] args) {
// number of random indices
int m = Integer.parseInt(args[0]);
// read in frequency of occurrence of n values
int n = args.length;
int[] freq = new int[n];
for (int i = 1; i < n; i++) {
freq[i] = Integer.parseInt(args[i]);
}
// compute total count of all frequencies
int total = 0;
for (int i = 1; i < n; i++) {
total += freq[i];
}
double[] summedProbabilities = new double[n];
for (int i = 1; i < summedProbabilities.length; i++) {
final double probability = freq[i] / (double) total;
summedProbabilities[i] = summedProbabilities[i -1] + probability;
}
for (int j = 0; j < m; j++) {
// generate random integer with probability proportional to frequency
double randomProbability = Math.random();
int i = 1;
while (randomProbability > summedProbabilities[i]) {
i++;
}
System.out.print(i + " ");
if (j % 10 == 0) {
System.out.println();
}
}
}
I strongly suggest you to refactor the code in a way that you use methods to calculate small pieces and compose it then.
答案2
得分: 0
以下是翻译好的代码部分:
public class DiscreteDistribution {
public static void main(String[] args) {
// 获取循环打印索引的次数
int m = Integer.parseInt(args[0]);
// 设置数组大小为命令行输入的数量减去第一个输入
// 因为我们不考虑 args[0] 处的输入
int[] n = new int[args.length - 1];
// 用于存储累积值的 cSum 数组
int[] cSum = new int[args.length];
// 用于存储随机生成的大小为 m 的数字数组
int[] rand = new int[m];
int count = 1;
int cCount = 1;
int sum = 0; // 用于累加输入 n 的值
// 将用户输入存储在数组 n 中,忽略第一个输入
for (int i = 0; i < n.length; i++) {
n[i] = Integer.parseInt(args[count]);
count++;
}
// 将 n 的累积值存储在 cSum 中
for (int j = 0; j < n.length; j++) {
sum = sum + n[j];
cSum[j + 1] = sum;
}
// 生成一个随机数并将其存储在 rand 数组中,表示概率
for (int p = 0; p < m; p++) {
int r = (int) (1 + Math.random() * cSum[cSum.length - 1]);
rand[p] = r;
}
// 循环从 0 到 m 打印 n 的索引
for (int s = 0; s < m; s++) {
// 根据条件打印对应的索引
for (int q = 1; q < n.length; q++) {
if (rand[s] <= cSum[q - 1]) {
System.out.print(q + " ");
}
}
}
}
}
英文:
public class DiscreteDistribution{
public static void main(String[] args) {
// takes in number of times we must loop to print indices
int m = Integer.parseInt(args[0]);
// set the array size to the number of input from command line
//minus the first input
//because we are not considering the input at args[0]
int [] n = new int[args.length-1];
// cSum to store the cummulatives
int [] cSum = new int[args.length];
// to store an array of random generated
//number of size m
int [] rand = new int[m];
int count = 1;
int cCount = 1;
int sum = 0; // to add the inputs n;
// to store user input in an array in n ignoring the first input
for(int i =0; i < n.length; i++)
{
n[i] = Integer.parseInt(args[count]);
count++;
}
//stores the cummulatives of n in cSum
for(int j = 0; j < n.length; j++)
{
sum = sum + n[j];
cSum[j+1] = sum;
}
//generate a random number and stores in rand representing the //probabilites
for(int p = 0; p < m; p++) {
int r = (int)(1+Math.random()*cSum[cSum.length-1]);
rand = r;
}
// loop from 0 to m to print the indices of n;
for(int s = 0; s < m; s++) {
//prints the indices corresponding to the condition
for(int q = 1; q < n.length; q++) {
if(rand
展开收缩 <= cSum[q-1]) {
System.out.print(q+" ");
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论