| Author |
Unique Random Number Generator : Please suggest improvements to this code
|
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
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.
|
SCJP 6. Learning more now.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2929
|
|
|
Your code generates only int random numbers and that would get exhausted soon. There are random number generation algorithms which you can implement.
|
Mohamed Sanaulla | My Blog
|
 |
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
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.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2929
|
|
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
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
A few line by line remarks about your code.
Line 9: Why is the variable temp not private? Why is it called temp? It would be better to give it a more meaningful name.
Line 39, 41: Don't compare booleans with == true or == false. Just write:
For the rest, the code seems OK.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
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
That is why you always get 4 last from 0 to 4.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
. . . and there is no need for the assignment before the remove() call: read this about the remove() method.
|
 |
Rahul Sudip Bose
Ranch Hand
Joined: Jan 21, 2011
Posts: 637
|
|
Thanks for analyzing the code. Working on it now.
Campbell Ritchie wrote:
There is an inconsistency when you enter a range smaller than 5
This problem is caused only because of the limits in the for-loop in main(). The replacement is :
There is 1 small problem with this snippet, at the end you will get -1 also.
PS : can this program be of any use besides filling an array in random order ?
will rectify the other things.
|
 |
 |
|
|
subject: Unique Random Number Generator : Please suggest improvements to this code
|
|
|