I am a newbie to java and I made this little program called "UniqueRandom". Please suggest ways to improve this code or point out any bad practices, errors etc.

INTRO :

Aim : This class CAN be used to generate each number from a given range only once in a random order, ie not necessarily increasing or decreasing.
______________________________________________________________________________________________________________________________________

HOW IT WORKS :

public void setData(int first, int last) -Takes range from user.
-fills the numbers in that range into an array list.

public int random( ) -math.random() picks an index ("rand") from above array list and stores the number at rand in a temporary variable "num" .
Then it deletes the number at index rand. Return num.
-call this method as many times as you like, provided you dont demand more numbers than are in the generator.

main() to test the code.
______________________________________________________________________________________________________________________________________

THE CODE :

THE OUTPUT :

thanks for reading patiently.
regards
rb

PS: Taken great pains to make this as convenient to read as possible. Please put a small reminder in your mind and review this if you can find the time or are in the mood for random stuff.

Mohamed Sanaulla wrote:Your code generates only int random numbers and that would get exhausted soon. There are random number generation algorithms which you can implement.

where can i get such algos ? Are they "easy" (ie no need to study other things as prerequisites) ?
One possible use of limited range is when you want to fill an (big) array/list with unique numbers instead of relying on math.random which can generate the same number again. eg range = 30 to 40 used with just math.random() could generate 30, 45 etc more than once.

thanks
rb

PS : can anyone imagine a real-world/useful application of this simple code ? maybe its just a kiddie code and not really useful.

Rahul Sudip Bose wrote:where can i get such algos ? Are they "easy" (ie no need to study other things as prerequisites) ?

Google is your friend Most of the books on algorithms will have this. Might need bit of math for this. Usually such algorithms would be part of curriculum

Why have you no constructor in that class; if you have instance fields, you ought to be using a constructor. Then you will always find isDataSet is true. I think you have misunderstood assertions; they are for testing in development. If an assertion fails, then you have an error in the code which needs to be corrected, or a precondition applied, so as to maintain the class invariants. The assertion is only enabled if you use the -ea option when executing the application.
You are not actually returning random numbers: you are selecting randomly from a declining population.
There is an inconsistency when you enter a range smaller than 5

java UniqueRandom
Enter the First number !
-1 2
Enter the Last number !
-1 0 1 2
Random num chosen is 0
Random num chosen is -1
Random num chosen is 1
Random num chosen is 2
You asked for more unique numbers than i can be generate !
Sorry, returning -1 !
Random num chosen is -1

You are returning -1 as an error state when -1 is part of the input range. You will notice I entered the last number before the prompt, and the Scanner read it correctly.
Why are you using the size() - 1? The -1 looks incorrect to me. You need to read the details of the Math#random() method

java UniqueRandom
Enter the First number !
0 4
Enter the Last number !
0 1 2 3 4
Random num chosen is 2
Random num chosen is 3
Random num chosen is 1
Random num chosen is 0
Random num chosen is 4