卡在离散分布上,问题出在无法完成这个问题。

huangapple go评论115阅读模式
英文:

Stuck on Discrete distribution having an issue with finishing the problem

问题

这是我应该得到的内容。

这是我实际得到的内容。

编写一个名为 DiscreteDistribution.java 的程序,它接受一个整数命令行参数 m,后面跟着一系列正整数命令行参数 a1,a2,…,an,然后打印 m 个随机索引(用空格分隔),每个索引 i 的选择概率与 ai 成比例。

到目前为止,我已经有了

  1. public static void main(String[] args) {
  2. // 随机索引的数量
  3. int m = Integer.parseInt(args[0]);
  4. // 读取 n 个值的出现频率
  5. int n = args.length;
  6. int[] freq = new int[n];
  7. for (int i = 0; i < n; i++) {
  8. freq[i] = Integer.parseInt(args[i]);
  9. }
  10. // 计算所有频率的总计数
  11. int total = 0;
  12. for (int i = 0; i < n; i++) {
  13. total += freq[i];
  14. }
  15. for (int j = 0; j < m; j++) {
  16. // 生成与频率成比例的随机整数
  17. int r = (int) ((total) * Math.random() - 1); // 在 [0, total) 范围内的整数
  18. int sum = 0;
  19. int event = -1;
  20. for (int i = 0; i < n && sum <= r; i++) {
  21. sum += freq[i];
  22. event = i;
  23. System.out.println(freq[i]);
  24. }
  25. }
  26. }
英文:

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

  1. public static void main(String[] args) {
  2. // number of random indices
  3. int m = Integer.parseInt(args[0]);
  4. // read in frequency of occurrence of n values
  5. int n = args.length;
  6. int[] freq = new int[n];
  7. for (int i = 0; i &lt; n; i++) {
  8. freq[i] = Integer.parseInt(args[i]);
  9. }
  10. // compute total count of all frequencies
  11. int total = 0;
  12. for (int i = 0; i &lt; n; i++) {
  13. total += freq[i];
  14. }
  15. for (int j = 0; j &lt; m; j++) {
  16. // generate random integer with probability proportional to frequency
  17. int r = (int) ((total) * Math.random() - 1); // integer in [0, total)
  18. int sum = 0;
  19. int event = -1;
  20. for (int i = 0; i &lt; n &amp;&amp; sum &lt;= r; i++) {
  21. sum += freq[i];
  22. event = i;
  23. System.out.println(freq[i]);
  24. }
  25. }
  26. }

答案1

得分: 1

根据我正确理解您的问题的假设,您可以使用以下算法根据给定的频率在范围从1到n内生成m个随机数:

  1. public static void main(String[] args) {
  2. // 随机数索引的数量
  3. int m = Integer.parseInt(args[0]);
  4. // 读取n个值的出现频率
  5. int n = args.length;
  6. int[] freq = new int[n];
  7. for (int i = 1; i < n; i++) {
  8. freq[i] = Integer.parseInt(args[i]);
  9. }
  10. // 计算所有频率的总计数
  11. int total = 0;
  12. for (int i = 1; i < n; i++) {
  13. total += freq[i];
  14. }
  15. double[] summedProbabilities = new double[n];
  16. for (int i = 1; i < summedProbabilities.length; i++) {
  17. final double probability = freq[i] / (double) total;
  18. summedProbabilities[i] = summedProbabilities[i - 1] + probability;
  19. }
  20. for (int j = 0; j < m; j++) {
  21. // 生成具有与频率成比例的概率的随机整数
  22. double randomProbability = Math.random();
  23. int i = 1;
  24. while (randomProbability > summedProbabilities[i]) {
  25. i++;
  26. }
  27. System.out.print(i + " ");
  28. if (j % 10 == 0) {
  29. System.out.println();
  30. }
  31. }
  32. }

我强烈建议您以一种使用方法来计算小块并将其组合在一起的方式重构代码。

英文:

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:

  1. public static void main(String[] args) {
  2. // number of random indices
  3. int m = Integer.parseInt(args[0]);
  4. // read in frequency of occurrence of n values
  5. int n = args.length;
  6. int[] freq = new int[n];
  7. for (int i = 1; i &lt; n; i++) {
  8. freq[i] = Integer.parseInt(args[i]);
  9. }
  10. // compute total count of all frequencies
  11. int total = 0;
  12. for (int i = 1; i &lt; n; i++) {
  13. total += freq[i];
  14. }
  15. double[] summedProbabilities = new double[n];
  16. for (int i = 1; i &lt; summedProbabilities.length; i++) {
  17. final double probability = freq[i] / (double) total;
  18. summedProbabilities[i] = summedProbabilities[i -1] + probability;
  19. }
  20. for (int j = 0; j &lt; m; j++) {
  21. // generate random integer with probability proportional to frequency
  22. double randomProbability = Math.random();
  23. int i = 1;
  24. while (randomProbability &gt; summedProbabilities[i]) {
  25. i++;
  26. }
  27. System.out.print(i + &quot; &quot;);
  28. if (j % 10 == 0) {
  29. System.out.println();
  30. }
  31. }
  32. }

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

以下是翻译好的代码部分:

  1. public class DiscreteDistribution {
  2. public static void main(String[] args) {
  3. // 获取循环打印索引的次数
  4. int m = Integer.parseInt(args[0]);
  5. // 设置数组大小为命令行输入的数量减去第一个输入
  6. // 因为我们不考虑 args[0] 处的输入
  7. int[] n = new int[args.length - 1];
  8. // 用于存储累积值的 cSum 数组
  9. int[] cSum = new int[args.length];
  10. // 用于存储随机生成的大小为 m 的数字数组
  11. int[] rand = new int[m];
  12. int count = 1;
  13. int cCount = 1;
  14. int sum = 0; // 用于累加输入 n 的值
  15. // 将用户输入存储在数组 n 中,忽略第一个输入
  16. for (int i = 0; i < n.length; i++) {
  17. n[i] = Integer.parseInt(args[count]);
  18. count++;
  19. }
  20. // 将 n 的累积值存储在 cSum 中
  21. for (int j = 0; j < n.length; j++) {
  22. sum = sum + n[j];
  23. cSum[j + 1] = sum;
  24. }
  25. // 生成一个随机数并将其存储在 rand 数组中,表示概率
  26. for (int p = 0; p < m; p++) {
  27. int r = (int) (1 + Math.random() * cSum[cSum.length - 1]);
  28. rand[p] = r;
  29. }
  30. // 循环从 0 到 m 打印 n 的索引
  31. for (int s = 0; s < m; s++) {
  32. // 根据条件打印对应的索引
  33. for (int q = 1; q < n.length; q++) {
  34. if (rand[s] <= cSum[q - 1]) {
  35. System.out.print(q + " ");
  36. }
  37. }
  38. }
  39. }
  40. }
英文:
  1. public class DiscreteDistribution{
  2. public static void main(String[] args) {
  3. // takes in number of times we must loop to print indices
  4. int m = Integer.parseInt(args[0]);
  5. // set the array size to the number of input from command line
  6. //minus the first input
  7. //because we are not considering the input at args[0]
  8. int [] n = new int[args.length-1];
  9. // cSum to store the cummulatives
  10. int [] cSum = new int[args.length];
  11. // to store an array of random generated
  12. //number of size m
  13. int [] rand = new int[m];
  14. int count = 1;
  15. int cCount = 1;
  16. int sum = 0; // to add the inputs n;
  17. // to store user input in an array in n ignoring the first input
  18. for(int i =0; i &lt; n.length; i++)
  19. {
  20. n[i] = Integer.parseInt(args[count]);
  21. count++;
  22. }
  23. //stores the cummulatives of n in cSum
  24. for(int j = 0; j &lt; n.length; j++)
  25. {
  26. sum = sum + n[j];
  27. cSum[j+1] = sum;
  28. }
  29. //generate a random number and stores in rand representing the //probabilites
  30. for(int p = 0; p &lt; m; p++) {
  31. int r = (int)(1+Math.random()*cSum[cSum.length-1]);
  32. rand

    = r;

  33. }

  34. // loop from 0 to m to print the indices of n;

  35. for(int s = 0; s &lt; m; s++) {

  36. //prints the indices corresponding to the condition

  37. for(int q = 1; q &lt; n.length; q++) {

  38. if(rand

    展开收缩
    &lt;= cSum[q-1]) {
  39. System.out.print(q+&quot; &quot;);
  40. }
  41. }
  42. }
  43. }

huangapple
  • 本文由 发表于 2020年5月3日 20:56:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/61574829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定