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: -->


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

Forum Jump:


Users browsing this thread: 1 Guest(s)