Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RandomNumberGenerator
#1
This code will generate 100 random numbers (can be changed) from 0 to 6. Then it will count how many of each number there was. This is a take on Brandon's Random Number Generator on YouTube

------------------------------------------------------
[code2=java]import java.util.Random;
public class GnrtngRndmNmbrs {

public static void main(String[] args){
int one = 0;
int two = 0;
int three = 0;
int four = 0;
int five = 0;
int six = 0;
int nill = 0;
int i = 0;
/*To change the amount of random numbers generated
* change the statement below from
* while (i <= 100); to
* while (i <= YourAmountOfNumbers);
*/
while (i <= 100){
Random rnd = new Random();


System.out.println(rnd.nextInt(7));


if ((rnd.nextInt(7) > 0.99) & rnd.nextInt(7) < 1.1){
one = one + 1;
}
if ((rnd.nextInt(7) > 1.99) & rnd.nextInt(7) < 2.1){
two = two + 1;
}
if ((rnd.nextInt(7) > 2.99) & rnd.nextInt(7) < 3.1){
three = three + 1;
}
if ((rnd.nextInt(7) > 3.99) & rnd.nextInt(7) < 4.1){
four = four + 1;
}
if ((rnd.nextInt(7) > 4.99) & rnd.nextInt(7) < 5.1){
five = five + 1;
}
if ((rnd.nextInt(7) > 5.99) & rnd.nextInt(7) < 6.1){
six = six + 1;
}
if (rnd.nextInt(7) < 0.1){
nill = nill + 1;
}
i++;
}

System.out.println("There were " + one + " ones");
System.out.println("There were " + two + " twos");
System.out.println("There were " + three + " threes");
System.out.println("There were " + four + " fours");
System.out.println("There were " + five + " fives");
System.out.println("There were " + six + " sixes");
System.out.println("There were " + nill + " zeros");
}

}[/code2]
------------------------------------------------------
Have fun! <!-- s:lol: --><img src="{SMILIES_PATH}/icon_lol.gif" alt=":lol:" title="Laughing" /><!-- s:lol: -->
#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.
#3
At the time of writing it i didn't know about arrays.
#4
WitherSlayer Wrote:At the time of writing it i didn't know about arrays.
Ah okay, well that would make sense! Good job, nonetheless!
My Blog | My Setup | My Videos | Have a wonderful day.


Forum Jump:


Users browsing this thread: 1 Guest(s)