Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RandomNumberGenerator
#2
Awesome work!

You could improve this program by using an array to keep track of how many of each number there are, however. Like so:
[code2=java]import java.util.Random;
public class RandomNumForum {

private static final int RANDOM_COUNT = 100;

public static void main(String[] args){
int intCount[] = new int[7];
int i = 0;
/*To change the amount of random numbers generated
* change the RANDOM_COUNT var
* to your amount of numbers
*/
while (i < RANDOM_COUNT){
Random rnd = new Random();


int randomInteger = rnd.nextInt(7);
System.out.println(randomInteger);



intCount[randomInteger]++;
i++;
}

System.out.println("There were " + intCount[1] + " ones");
System.out.println("There were " + intCount[2] + " twos");
System.out.println("There were " + intCount[3] + " threes");
System.out.println("There were " + intCount[4] + " fours");
System.out.println("There were " + intCount[5] + " fives");
System.out.println("There were " + intCount[6] + " sixes");
System.out.println("There were " + intCount[0] + " zeros");
}

}[/code2]
My Blog | My Setup | My Videos | Have a wonderful day.


Messages In This Thread
RandomNumberGenerator - by WitherSlayer - 01-03-2013, 02:09 AM
Re: RandomNumberGenerator - by brandonio21_phpbb3_import2 - 01-03-2013, 11:45 AM
Re: RandomNumberGenerator - by WitherSlayer - 01-03-2013, 04:35 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)