使用java生成随机数直方图

发布于 2024-08-11 05:43:15 字数 3101 浏览 10 评论 0原文

上面的直方图

--------------------------------------------------------
  1 ****(4)
  2 ******(6)
  3 ***********(11)
  4 *****************(17)
  5 **************************(26)
  6 *************************(25)
  7 *******(7)
  8 ***(3)
  9 (0)
 10 *(1)
--------------------------------------------------------

基本上就是我的程序需要做的事情..我在某处遗漏了一些东西,任何帮助都会很棒:)

import java.util.Random; 
public class Histogram
{

    /*This is a program to generate random number histogram between
    1 and 100 and generate a table */

    public static void main(String args[])
    {

        int [] randarray = new int [80];
        Random random = new Random();
        System.out.println("Histogram");
        System.out.println("---------");

        int i ;
        for ( i = 0; i<randarray.length;i++)
        {   
            int temp = random.nextInt(100); //random numbers up to number value 100
            randarray[i] = temp;

        }

        int [] histo = new int [10];
        for ( i = 0; i<10; i++)
        {
            /* %03d\t, this generates the random numbers to
            three decimal places so the numbers are generated
            with a full number or number with 00's or one 0*/


            if (randarray[i] <= 10) {
                histo[i] = histo[i] + 1;
            //System.out.println("*");
            }
            else if ( randarray[i] <= 20){
            histo[i] = histo[i] + 1;
            }
            else if (randarray[i] <= 30){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <= 40){
            histo[i] = histo[i] + 1;
            }
            else if (randarray[i] <= 50){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=60){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=70){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=80){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=90){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=100){
            histo[i] = histo[i] + 1;
            }

            switch (randarray[i])
            {
            case 1: System.out.print("0-10 | "); break;
            case 2: System.out.print("11-20 | "); break;
            case 3: System.out.print("21-30 | "); break;
            case 4: System.out.print("31-40 | "); break;
            case 5: System.out.print("41-50 | "); break;
            case 6: System.out.print("51-60 | "); break;
            case 7: System.out.print("61-70 | "); break;
            case 8: System.out.print("71-80 | "); break;
            case 9: System.out.print("81-90 | "); break;
            case 10: System.out.print("91-100 | "); 
            }
                for (int i = 0; i < 80; i++)
            {
              randomNumber = random.nextInt(100)
              index = (randomNumber - 1) / 2;
              histo[index]++;
            }
    }
   }
 }

Histogram

--------------------------------------------------------
  1 ****(4)
  2 ******(6)
  3 ***********(11)
  4 *****************(17)
  5 **************************(26)
  6 *************************(25)
  7 *******(7)
  8 ***(3)
  9 (0)
 10 *(1)
--------------------------------------------------------

basically above is what my prgram needs to do.. im missing something somewhere any help would be great :)

import java.util.Random; 
public class Histogram
{

    /*This is a program to generate random number histogram between
    1 and 100 and generate a table */

    public static void main(String args[])
    {

        int [] randarray = new int [80];
        Random random = new Random();
        System.out.println("Histogram");
        System.out.println("---------");

        int i ;
        for ( i = 0; i<randarray.length;i++)
        {   
            int temp = random.nextInt(100); //random numbers up to number value 100
            randarray[i] = temp;

        }

        int [] histo = new int [10];
        for ( i = 0; i<10; i++)
        {
            /* %03d\t, this generates the random numbers to
            three decimal places so the numbers are generated
            with a full number or number with 00's or one 0*/


            if (randarray[i] <= 10) {
                histo[i] = histo[i] + 1;
            //System.out.println("*");
            }
            else if ( randarray[i] <= 20){
            histo[i] = histo[i] + 1;
            }
            else if (randarray[i] <= 30){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <= 40){
            histo[i] = histo[i] + 1;
            }
            else if (randarray[i] <= 50){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=60){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=70){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=80){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=90){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=100){
            histo[i] = histo[i] + 1;
            }

            switch (randarray[i])
            {
            case 1: System.out.print("0-10 | "); break;
            case 2: System.out.print("11-20 | "); break;
            case 3: System.out.print("21-30 | "); break;
            case 4: System.out.print("31-40 | "); break;
            case 5: System.out.print("41-50 | "); break;
            case 6: System.out.print("51-60 | "); break;
            case 7: System.out.print("61-70 | "); break;
            case 8: System.out.print("71-80 | "); break;
            case 9: System.out.print("81-90 | "); break;
            case 10: System.out.print("91-100 | "); 
            }
                for (int i = 0; i < 80; i++)
            {
              randomNumber = random.nextInt(100)
              index = (randomNumber - 1) / 2;
              histo[index]++;
            }
    }
   }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

假情假意假温柔 2024-08-18 05:43:15

您的随机数据包含 80 个值,但您仅迭代前 10 个值。您应该迭代所有 80 个值。您可以使用 histo[1]、histo[2] 等而不是 histo[i]。

此外,整个大 switch 块可以简化为

histo[randarray[i] / 10]++;

并且您可以简单地执行以下操作,而不是创建 randarray 然后循环它:

for(int i = 0; i < 80; i++)
{
    histo[random.nextInt(100) / 10]++;
}

Your random data contains 80 values but you are only iterating through the first 10. You should be iterating through all 80. You would use histo[1], histo[2], etc instead of histo[i].

Also, the whole big switch block could be simplified into

histo[randarray[i] / 10]++;

And instead of creating the randarray and then looping through it, you could simply do this:

for(int i = 0; i < 80; i++)
{
    histo[random.nextInt(100) / 10]++;
}
情深已缘浅 2024-08-18 05:43:15

尽管数组中有 80 个随机数,但您只循环了 10 次。

这条线也是有缺陷的。您不想使用相同的计数器变量来遍历数组并确定要递增的直方图箱。

 if (randarray[i] <= 10) {
                        histo[i] = histo[i] + 1;

更新:

您应该尝试提出一种算法将随机值转换为垃圾箱,因为您的解决方案不可扩展,并且您将习惯不良的编程习惯。

You are only looping through 10 times, though you have 80 random numbers in your array.

This line is also flawed. You don't want to use the same counter variable for going through the array and determining which histogram bin to increment.

 if (randarray[i] <= 10) {
                        histo[i] = histo[i] + 1;

UPDATE:

You should try to come up with an algorithm to turn a random value into a bin, as your solution isn't scalable, and you will be getting used to bad programming habits.

皓月长歌 2024-08-18 05:43:15

你的最后一个 for 循环将无法编译 - 那里有很多语法错误。

您需要用随机数填充历史记录,然后将其打印出来。

在中断之前,您还想在每个 case 语句中打印出 hist 数组中的计数。

如果您继续学习编程,祝您好运!

Your last for loop won't compile - there are a lot of syntax errors there.

You need to fill the hists with your random numbers, THEN print them out.

You want to print the counts in your hist array out in each case statement before you break as well.

Good luck if you continue to learn programming!

桃气十足 2024-08-18 05:43:15

需要考虑的另一点是,您的直方图将是平坦、均匀的分布,而不是像您在问题中显示的正态分布。

Another point to consider is that your histogram will be a flat, uniform distribution, not a normal distribution like you show in your question.

删除会话 2024-08-18 05:43:15

如果我没看错的话,那么我认为你的 switch 语句搞砸了。

您的 randarray 值来自 0 0 0 0 < randarray[i] < 100,但您只为 switch 指定最多 10 的值。这可能会导致一些问题。

只是我的猜测。

If I read that right, then I think your switch statement is messed up.

Your randarray values are from 0 < randarray[i] < 100 but you are only giving values up to 10 for your switch. That would probably throw something off.

Just my guess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文