I need to generate a random integer with Java but random in a one sided bounded specific range. For example, a range from 15+ means that the only constraint is that the lowest value the integer can take is 15.
Asked
Active
Viewed 2,736 times
2
-
2Surely you mean 15 to Integer.MAX_VALUE? I'm hoping now I've reminded you an Integer has a maximum value, you will realise the solution yourself. ;) – Guy Flavell Sep 16 '14 at 18:11
-
1Well, we now know what you want, but what have _you_ tried so far to generate such a number? – pzaenger Sep 16 '14 at 18:11
-
Oh, OP. Could you clarify some edge cases: Can it have an upper bound? e.g. 15-, and are negatives valid outputs? – Guy Flavell Sep 16 '14 at 18:23
-
1Do you mean a *uniform* distribution? That's pretty simple. If you're talking about truncating another distribution, like the normal distribution, that's more interesting. – Mike Dunlavey Sep 16 '14 at 18:35
2 Answers
3
Random rand = new Random();
int min=15;
int randomNum = rand.nextInt((2147483647 - min) + 1)+ min;

Madhawa Priyashantha
- 9,633
- 7
- 33
- 60
1
You can create a simple randomInRange
function like this:
Yyou want to create the Random
object only once so it doesn't have to re-seed each time you call the randomInRange()
function.
Random rand;
// ...
// where you initialize stuff (for example the class constructor)
rand = new Random();
// ...
int randomInRange(int min, int max) {
return rand.nextInt((max - min) + 1) + min;
}
If you want to only have the minimum value, maybe create another method like:
int randomFrom(int min) {
return randomInRange(min, Integer.MAX_VALUE);
}
I suggest you make a separate Utilities
class that will contain these methods as static
and you can call them by saying Utilities.randomInRange()
You can of course make them static
in your class as well if these are the only utility methods you will need

nem035
- 34,790
- 6
- 87
- 99
-
3Didn't not copy properly from ... http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java – StackFlowed Sep 16 '14 at 18:14
-
-
@Aeshang this is a standard way of generating random numbers in java between a certain range.... – nem035 Sep 16 '14 at 18:15
-
@GuyFlavell I understand that but it is just a simple answer. I will edit that part now – nem035 Sep 16 '14 at 18:16
-
@nem use static in you method ... :P that is why i said didn't copy properly ;) – StackFlowed Sep 16 '14 at 18:16
-
@nem you may still get hate from answering this question which appears to be simple enough to be a homework. ;) (but not from me) – Guy Flavell Sep 16 '14 at 18:19
-
@GuyFlavell ok... don't really understand you guys, especially the approval of the second answer that is pretty much equivalent to mine :) as far as creating the new `Random` each time, I realize that is not efficient – nem035 Sep 16 '14 at 18:23
-
The second answer is different in that it doesn't attempt to provide a full solution, if the new Random() is contained in the same method it would still be wrong. But it is sufficiently incomplete to be ambiguous. :) I am happy to upvote you when you edit this to be correct. :) It's meant as guidance, not punishment. :) – Guy Flavell Sep 16 '14 at 18:25
-
@GuyFlavell it makes sense, just wasn't aware of the whole "not providing complete answers" but ok, cool... I though giving a step by step answer is more beneficial than a hint. However, I see your point. thanks for the advice then :) – nem035 Sep 16 '14 at 18:29
-
@Aeshang I don't understand why something relatively simple and well-know like this has to be copied :) `static` isn't included cause I provided the simplest answer... Here, I added it, just for you :P – nem035 Sep 16 '14 at 18:38