• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unique Random Number Generator : Please suggest improvements to this code

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code generates only int random numbers and that would get exhausted soon. There are random number generation algorithms which you can implement.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and there is no need for the assignment before the remove() call: read this about the remove() method.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic