0

I'm having some trouble with a program that I'm writing. What I need to do is generate random numbers from 0-9, 100 times, and then print the outcome of how many times each numbers is generated. I have to put this all into arrays, one holding the 100 random numbers, and another holding the 'x' times each number was printed.

Here is my code so far:

import java.applet.Applet;
import java.awt.*;

public class RandomTölur extends Applet {

    TextArea t;
    int[] random;
    int[] hveOft;
    int i;
    int[] k = hveOft;
    public void init()
    {
        t = new TextArea(5, 25);
        this.add(t);

        random = new int[101];
        random[0] = 0;

        for(i=1; i<100; i++)
        {
            random[i] = random[i];
        }

        for(int k=1; k<10; k++)
        {
            t.appendText("Talan " + k + " kom " + random[i] + " sinnum" + "\n");
        }

    }
}

When it prints out "Talan 'k' kom 'i' sinnum", that means "the number 'k' was printed 'i' times.

The problem I'm having is that it doesnt print out how many times each number came out.

Is there anyone who can spot my error?

Ps. sorry if this post is unproffessional or something, this is my first post on Stackoverflow.

Thanks in advance! :)

Ps. I've figured this out thanks to the help of you guys, so thanks everyone who reply'd to this :) Much appriciated.

2
  • I'm guessing it prints "Talan k kom 0 sinnum" 9 times? this.random[this.i] = (int)(Math.random() * 10); will help. As will starting your final loop from 0, not 1 Commented Feb 20, 2014 at 16:29
  • Where would I put that line of code you wrote, and would I be replacing it for something I wrote? Commented Feb 20, 2014 at 16:34

4 Answers 4

1

create array for 0..9 numbers and set times to 0

random = new int[10];

for(i=0; i<10; i++)
{
    random[i] = 0;
}

and 100times generate number and increase number in array

Random rand = new Random();

for(i=0; i<100; i++)
{

    random[rand.nextInt(10)]++;
}
Sign up to request clarification or add additional context in comments.

1 Comment

No need to initialise the array like that. It will do it for you. I do like the idea of randomising the index though. +1 for that.
0

Using the things I noticed and some of the other answers here, I came up with this implementation:

public class Random extends Applet {
    TextArea t;
    int[] random;
    int[] hveOft;
    int i;
    int[] k = this.hveOft;
    @Override
    public void init()
    {
        this.t = new TextArea(25, 25); //Made the TextArea a bit bigger.
        this.add(this.t);

        this.random = new int[10]; //Defined the random array as size 10

        for(this.i=0; this.i<100; this.i++) { //looping from 0 to <100 will give you 100 random items
            //Used Math.random, but this is the same idea as Karci10
            this.random[(int)(Math.random() * 10)]++; 
        }

        for(int k=0; k<10; k++) { //Again, looping from 0 to get all 0-9 numbers
            //Don't forget to use k in this loop, not i as k is the one you're dealing with here.
            this.t.appendText("Talan " + k + " kom " + this.random[k] + " sinnum" + "\n");
        }

    }
}

1 Comment

As I say, the root of the problem was the random number bit and I took inspiration from @karci10 on that one.
0

There are some points in your code.

  • First of all you are never generating some kind of random numbers.
  • Second random[i] = random[i] assignes itself, so no change.
  • In your appendText call you using random[i] out of your for loop of i. Therefor here always random[100] is used.

Comments

0
int[] repetitions = new int[100];
int[] random = new random[100];
int auxiliar = 0;
for(int i=0; i<100; i++){
    Random t = new Random();
    auxiliar = t.nextInt(10);
    random[i] = auxiliar;
    repetitions[auxiliar] +=1;
}

for(int i=0; i<100; i++){
    t.appendText("Talan " + i + " kom " + repetitions[i] + " sinnum" + "\n");
}

I think that is what you need

This generates 100 random numbers that are stored in random, and the output is the number of times each number has appeared, even if the number appears 0 times.

If you don't need the numbers that appears 0 times you can change the second for for te next one:

for(int i=0; i<100; i++){
    if(repetitions[i] != 0){
        t.appendText("Talan " + i + " kom " + repetitions[i] + " sinnum" + "\n");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.