Flash Games
 FAQ   Search   Memberlist   Usergroups   Register  Profile   Log in to check your private messages   Log in 


How to Generate Unique Random Numbers?



 

Post new topic   Reply to topic  
   DEVPPL Forum Index -> Java Forum
View previous topic :: View next topic  
Author Message
randy_ortan



Joined: 11 Sep 2008
Posts: 2

PostPosted: Mon Sep 22, 2008 7:49 am    Post subject: How to Generate Unique Random Numbers? Reply with quote

Code:



import com.sun.crypto.provider.RSACipher;
import java.util.Random;

public class RandomRange {

    static  int START = 1;
    static int END;
   static  Random random = new Random();
 
    private static void showRandomInteger(int aStart, int aEnd, Random aRandom){
            if ( aStart > aEnd ) {
                throw new IllegalArgumentException("Start cannot exceed End.");
            }
            //get the range, casting to long to avoid overflow problems
            long range = (long)aEnd - (long)aStart + 1;
            // compute a fraction of the range, 0 <= frac < range
            long fraction = (long)(range * aRandom.nextDouble());
            int randomNumber =  (int)(fraction + aStart);                     
            //  System.out.println("Hi this is from senthil == >"+ j );
            log("Generated : " + randomNumber);
    }
   
    private static void log(String aMessage){
        System.out.println(aMessage);
    }   
   
    public static void main(String aArgs[]){
        int countrec = 9;
        log("Generating random integers in the range "+START+"...." + countrec);
        END = countrec - 1;
      for (int idx = START; idx <= 5; ++idx){
            showRandomInteger(START, END, random);
        }
        log("Done.");
       
       
    }
}



Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    DEVPPL Forum Index -> Java Forum All times are GMT + 1 Hour
Page 1 of 1

 
 
Welcome to DEVPPL.com
You are not logged in, which means that you can't post in the forums.
Click here to Register

If you are a current member here on DEVPPL, please login below:

User: Pass:
Log me on automatically each visit:

 


Powered by phpBB © 2001, 2005 phpBB Group - Modified by DEVPPL

Flash Games - Sitemap