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.");
        
        
    }
}

Recommended Answers

All 3 Replies

What is your question? You didn't specify any portion of the code that you want us to look at. Do you just want us to give you random number generating techniques in general, because for that, you can use google.

building a string from 4 unique number[0-9] (i hope :) )

it's messy but it seems to work.

private String buildNumber() { //generate 4 unique numbers
		int n[] = new int[4];
		int goodNumber = 1;
		Random randomGenerator = new Random();
		while (goodNumber < 5) {
			for (int i = 0; i < 4; i++) {
				n[i] = randomGenerator.nextInt(10);
				if (i > 0) {
					for (int j = 0; j < i; j++) {
						if (n[i] == n[j]) {
							goodNumber = 1;
							while (n[j] == n[i]) {
								n[i] = randomGenerator.nextInt(10);
							}
							j = 0;
						} else {
							goodNumber++;
						}
					}
				}
			}
		}
		return String.valueOf(n[0]) + String.valueOf(n[1])
				+ String.valueOf(n[2]) + String.valueOf(n[3]);
	}

And why would you ever do that instead of generating a random number and using the + operator?

Also, don't up year old threads. Then I look at my replies and wonder why I can't remember ever saying them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.