I tried the solution here. However, I don't understand what the rand()
part is, which is too bad since it seemed really simple with just one line of code.
I tried the solution here. However, I don't understand what the rand()
part is, which is too bad since it seemed really simple with just one line of code.
Can you review my random Random number wrapper class
I made this class to handle any min max integer value and return a random number in the entered parameter range. I tested it to work with all combinations(I think). But is this totally wrong or written with unnecessary amounts of code? I mean areAre there obvious javaJava convention violations or obvious redundancy in this?
I was thinkingwondering if it was possible to do the same without instatiaing Randominstantiating Random
, since I have read that object instantiation is more ressourceresource demanding than method invoking, like invoking MathMath.random()
.random() I just couldn't make that work, unfortunately, as I didn't save the strange non-working code that that ended with.
I tried the solution here: http://stackoverflow.com/questions/3383286/create-a-random-even-number-between-rangehere. However, I don't understand what the "rand()"rand()
part is, which is too bad since it seemed really simple with just one line of code.
Can you review my random number wrapper class
I made this class to handle any min max integer value and return a random number in the entered parameter range. I tested it to work with all combinations(I think). But is this totally wrong or written with unnecessary amounts of code? I mean are there obvious java convention violations or obvious redundancy in this?
I was thinking if it was possible to do the same without instatiaing Random, since I have read that object instantiation is more ressource demanding than method invoking, like invoking Math.random() I just couldn't make that work, unfortunately I didn't save the strange non-working code that that ended with.
I tried the solution here: http://stackoverflow.com/questions/3383286/create-a-random-even-number-between-range. However I don't understand what the "rand()" part is, which is too bad since it seemed really simple with just one line of code.
Random number wrapper class
I made this class to handle any min max integer value and return a random number in the entered parameter range. I tested it to work with all combinations(I think). But is this totally wrong or written with unnecessary amounts of code? Are there obvious Java convention violations or obvious redundancy in this?
I was wondering if it was possible to do the same without instantiating Random
, since I have read that object instantiation is more resource demanding than method invoking, like invoking Math.random()
. I just couldn't make that work, unfortunately, as I didn't save the strange non-working code that that ended with.
I tried the solution here. However, I don't understand what the rand()
part is, which is too bad since it seemed really simple with just one line of code.
EDIT: I used the suggestion in the answer to create the following class and it works wonderfully, thanks to all who answered/commented.
BTW Is it true that not using a Random object improves perfomance? In my application I need to return many random numbers all the time, thats why I believe that using Math.floor and Math.random() is much better, true or false? ?
abstract class RandomInteger {
public static int returnRandomIntRange(int min, int max){
if(min > 2000 || max > 2000 || min < -2000 || max < -2000 || max < min){
throw new IllegalArgumentException("Min/max error: min/max out of range(not between -2000/2000) or min is higher than max .");
}
int random = (int) Math.floor(Math.random() * (max - min + 1) ) + min;
System.out.println("random: " + random);
return random;
}
}
EDIT: I used the suggestion in the answer to create the following class and it works wonderfully, thanks to all who answered/commented.
BTW Is it true that not using a Random object improves perfomance? In my application I need to return many random numbers all the time, thats why I believe that using Math.floor and Math.random() is much better, true or false? ?
abstract class RandomInteger {
public static int returnRandomIntRange(int min, int max){
if(min > 2000 || max > 2000 || min < -2000 || max < -2000 || max < min){
throw new IllegalArgumentException("Min/max error: min/max out of range(not between -2000/2000) or min is higher than max .");
}
int random = (int) Math.floor(Math.random() * (max - min + 1) ) + min;
System.out.println("random: " + random);
return random;
}
}