Reputation: 49
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.
Upvotes: 0
Views: 169
Reputation: 2766
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");
}
}
}
Upvotes: 0
Reputation: 213
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");
}
}
Upvotes: 0
Reputation: 375
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)]++;
}
Upvotes: 1
Reputation: 9201
There are some points in your code.
random[i] = random[i]
assignes itself, so no change. Upvotes: 0