Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to generate unique random numbers in java

  • In java we can generate random number in two ways 
  • By using Random class
  • By using Math.random



Program #1:  Java Example program to generate random numbers using random class within  the range of 1 to 10

  • First we need to create object of java.util.Random class.
  • After creating object of java.util.Random class then we need call nextInt() method by passing range
  • int range = maximum - minimum + 1;
  • int randomNum =  rn.nextInt(range) + minimum;

  1. package com.randomnumbergenerator;

  2. import java.util.Random;
  3. import java.util.Scanner;

  4. public class RandomNumber {

  5. /**
  6. * @Website: www.instanceofjava.com
  7. * @category: how to generate random numbers in java within range
  8. */
  9. public static void main(String[] args) {
  10. Scanner in = new Scanner(System.in);
  11. System.out.println("Enter minimum number");
  12. int minimum=in.nextInt();
  13. System.out.println("Enter maximum number");
  14. int maximum=in.nextInt();
  15. Random rn = new Random();
  16. int range = maximum - minimum + 1;
  17. int randomNum =  rn.nextInt(range) + minimum;
  18. System.out.println("Random Number= "+randomNum);

  19. }

  20. }

Output:

  1. Enter minimum number
  2. 1
  3. Enter maximum number
  4. 10
  5. Random Number= 4


Program #2:  Java Example program to generate random numbers using Math.random  within  the range of 1 to 10

  • By using Math.random() method also we can generate random number in java
  • int randomNum = minimum + (int)(Math.random() * maximum);

  1. package com.randomnumbergenerator;

  2. import java.util.Random;
  3. import java.util.Scanner;

  4. public class RandomNumber {

  5. /**
  6. * @Website: www.instanceofjava.com
  7. * @category: how to generate random numbers in java within range
  8. */
  9. public static void main(String[] args) {
  10. Scanner in = new Scanner(System.in);
  11. System.out.println("Enter minimum number");
  12. int minimum=in.nextInt();
  13. System.out.println("Enter maximum number");
  14. int maximum=in.nextInt();
  15.   
  16.   int randomNum = minimum + (int)(Math.random() * maximum);
  17. System.out.println("Random Number= "+randomNum);

  18. }

  19. }

Output:

  1. Enter minimum number
  2. 1
  3. Enter maximum number
  4. 10
  5. Random Number=5


Program #3:  Java Example program to generate 10 random numbers using Random class  within  the range of 1 to 100 using for loop.

  1. package com.randomnumbergenerator;

  2. import java.util.Random;
  3. import java.util.Scanner;

  4. public class RandomNumber {

  5. /**
  6. * @Website: www.instanceofjava.com
  7. * @category: how to generate random numbers in java within range
  8. */
  9. public static void main(String[] args) {
  10. Random randomNumGenerator = new Random();
  11.  
  12.            for (int idx = 1; idx
  13.               int randomInt = randomNumGenerator.nextInt(100);
  14.               System.out.println("Random Number= "+randomInt);
  15.  
  16. }       

  17. }

  18. }

Output:

  1. Random Number= 17
  2. Random Number= 3
  3. Random Number= 74
  4. Random Number= 59
  5. Random Number= 81
  6. Random Number= 90
  7. Random Number= 2
  8. Random Number= 32
  9. Random Number= 11
  10. Random Number= 75






How to generate random numbers in java without repetitions

  • lets see how to generate unique random numbers in java
  • By using Collections.shuffle();


Program #4:  Java Example program to generate 4 random numbers using Random class  within  the range of 1 to 100 without duplicate / java generate unique random number between 1 and 100


  1. package com.randomnumbergenerator;


  2. public class RandomNumber {

  3. /**
  4. * @Website: www.instanceofjava.com
  5. * @category: how to generate random numbers in java within range
  6. */
  7. public static void main(String[] args) {
  8.   ArrayList list = new ArrayList();
  9.             for (int i=1; i
  10.                 list.add(new Integer(i));
  11.             }
  12.             Collections.shuffle(list);
  13.             for (int i=0; i
  14.                 System.out.println("Random Number= "+(list.get(i)));
  15.             }
  16. }       

  17. }

  18. }

Output:

  1. Random Number= 7
  2. Random Number= 3
  3. Random Number= 2
  4. Random Number= 9

Program #5:  Java Example program to generate 10 random numbers. random number generator that doesn't repeat java

  1. package com.randomnumbergenerator;


  2. public class RandomNumber {

  3. /**
  4. * @Website: www.instanceofjava.com
  5. * @category: how to generate 10 random numbers in java within range
  6. */
  7.  
  8. public static final int SET_SIZE_REQUIRED = 10;
  9.  public static final int NUMBER_RANGE = 100;
  10.  
  11.  public static void main(String[] args) {
  12.  
  13.             Random random = new Random();
  14.  
  15.             Set set = new HashSet(SET_SIZE_REQUIRED);
  16.  
  17.             while(set.size()
  18.                 while (set.add(random.nextInt(NUMBER_RANGE)) != true)
  19.                     ;
  20.             }
  21.             assert set.size() == SET_SIZE_REQUIRED;
  22.             System.out.println(set);
  23.         }
  24. }


Output:

  1. [48, 99, 24, 58, 44, 77, 14, 95, 31, 79]


This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

How to generate unique random numbers in java

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×