Generate Unique Random

How to generate unique Random positive integers between two given numbers and all the numbers between two given numbers should be present.

Its like jumbling the numbers between 1 to 1000.

public class MyTest {
    public static void main(String args[]) {
        new MyTest().doit();
    }
    public void doit() {
      // no seed
      java.util.Random rand = new java.util.Random();
 //SEEDED
 // long seed = 10;  //I'd get this from data/time
      //Random rand  = new Random(seed);
      // random integer 0 to 10
      int max = 10;
      for (int i=0; i<10; i++) {
          System.out.println(rand.nextInt(max + 1));
      }
      // random double 0 to 10
      double maxd = 10;
      for (int i=0; i<10; i++) {
        System.out.println(maxd * rand.nextDouble());
        }
    }
}
 

public class MyTest {
    public static void main(String args[]) {
        new MyTest().doit();
    }
    public void doit() {
      java.util.Random rand = new java.util.Random();
      // random integer 0 to 10
      int max = 10;
      for (int i=0; i<10; i++) {
          System.out.println(rand.nextInt(max + 1));
      }
      // random double 0 to 10
      double maxd = 10;
      for (int i=0; i<10; i++) {
        System.out.println(maxd * rand.nextDouble());
        }
    }
}

Jonathan Roberts

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.