I have read other posts on this topic but I have not found an answer so far.
Given an array of length of 1000, find its lowest and highest values. These values are the bounds within which a random number is generated.
Finding lowest and highest numbers of an array is OK for me, but generating the number within those bounds is not.
Seems like a simple int number = random.nextInt(max - min) + min;
would do the trick, but it does not, because:
a) max can be 100 and min can be -100000, which becomes a negative bound and causes error b) other solutions like random.nextInt(Math.abs(min) + Math.abs(max)) - Math.abs(min)
simply fail, because within an integer data type, adding two positive numbers together can result in a negative value if the maximum value is exceeded.
So let's say I want to generate a random number within -2147483648 and 2147483647. How do I do that?
Thanks alot...
Aucun commentaire:
Enregistrer un commentaire