SCJP 6. Learning more now.
Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
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.
SCJP 6. Learning more now.
Rahul Sudip Bose wrote:where can i get such algos ? Are they "easy" (ie no need to study other things as prerequisites) ?
Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
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.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
That is why you always get 4 last from 0 to 4.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
Campbell Ritchie wrote:
There is an inconsistency when you enter a range smaller than 5
SCJP 6. Learning more now.
Consider Paul's rocket mass heater. |