Assignemnt #53
Code
/// Name: Daniel Nguyen
/// Period: 6
/// Program Name: Randomness Program
/// File Name: Randomness.java
/// Date Finished: 9/9/2015
// When running a program with a specific seed, all the random numbers stay the same no matter how much you run it
// changing the seed will change the numbers but still no matter how many times you run it, the numbers wont change.
import java.util.Random;
public class Randomness
{
public static void main ( String[] args )
{
Random r = new Random(12353);
int x = 1 + r.nextInt(10);
System.out.println( "My random number is " + x );
// Removing the "1 +" makes the range 0 - 4 stating 5 after nextInt creates a range of any positive interger below 5
// changing it to 3 + makes the range 3 - 7 because it moves all the numbers in 0 - 4 up 3
System.out.println( "Here are some numbers from 1 to 5!" );
System.out.print( r.nextInt(5) + " " );
System.out.print( r.nextInt(5) + " " );
System.out.print( r.nextInt(5) + " " );
System.out.print( r.nextInt(5) + " " );
System.out.print( r.nextInt(5) + " " );
System.out.print( r.nextInt(5) + " " );
System.out.println();
System.out.println( "Here are some numbers from 1 to 100!" );
System.out.print( 1 + r.nextInt(100) + "\t" );
System.out.print( 1 + r.nextInt(100) + "\t" );
System.out.print( 1 + r.nextInt(100) + "\t" );
System.out.print( 1 + r.nextInt(100) + "\t" );
System.out.print( 1 + r.nextInt(100) + "\t" );
System.out.print( 1 + r.nextInt(100) + "\t" );
System.out.println();
int num1 = 1 + r.nextInt(10);
int num2 = 1 + r.nextInt(10);
if ( num1 == num2 )
{
System.out.println( "The random numbers were the same! Weird." );
}
if ( num1 != num2 )
{
System.out.println( "The random numbers were different! Not too surprising, actually." );
}
}
}
Picture of the output