This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 3 years ago.
double random1 = Math.random() * 9 + -9;
double random2 = Math.random() * -9 + 9;
I need randomly generated numbers ranging from -9 to 9 using Math.random. When I use this code I have here it only prints the first number as a negative number.I've ran this multiple times and still the first number is always negative while the second is positive. How can I fix this to be more random? Thanks
Math.random() generates a number between 0 and 1.
How about multiplying by 18 and then subtracting 9?
From jdk-7 you can use ThreadLocalRandom
public int nextInt(int origin, int bound)
Returns a pseudorandom int value between the specified origin (inclusive) and the specified bound (exclusive).
ThreadLocalRandom.current().nextInt(-9, 10) // will generate from -9>=x<10
When you want a value between a number min and a number max :
Math.random() * ((max - min) + 1) + min
Related
This question already has answers here:
Getting N random numbers whose sum is M
(9 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
my output contains 3 textfields where when given N value (the doClick() function clicks 3 textfields automatically), then it randomly generates 3 numbers in the three textfields , My code is generating only random numbers , but i want those randomly generated numbers to be exactly add up to given N.
example : when N=20
then possible answers can be:
1.10,10,0 i.e. (textfield1 show 10, textfield2 show 10, textfiled3 show 0, whose sum adds up to given N )
2.15,3,2
3.10,5,5
random numbers can be any positive integer but it should add up to given N.
Any help please.
You could use Math.random() instead of the Random class. Math.random() returns a double between 0 and 1. So the only thing you have to do is multiply the result of Math.random() with N. The next number would be N minus your result from the subtraction of N and the previous result.
final int N = 20;
final int result0 = (int) (Math.random() * N);
final int result1 = (int) (Math.random() * (N - result0));
final int result2 = N - result0 - result1;
EDIT: You could than choose the first, second and third parameter randomly too.
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 3 years ago.
I am trying to print a random number between 80 and 100.
However when I print out this number I am getting numbers over 100?
Random num = new Random();
int percent = 80 + num.nextInt(100);
System.out.println(percent);
Can you try something like this
There are two approach below
int resultNumber = ThreadLocalRandom.current().nextInt(80, 100 + 1);
System.out.println(resultNumber);
Random num = new Random();
int min =80;
int max=100;
System.out.println(num.nextInt(max-min) + min);
Explanation
From the documentation of Random#nextInt(int):
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), [...]
So your call
num.nextInt(100);
Generates numbers between 0 (inclusive) and 100 (exclusive). After that you add 80 to it. The resulting range for your percent is thus 80 (inclusive) to 180 (exclusive).
Solution
In order to get a number from 80 to 100, what you actually want to do is to generate a number from 0 to 20 and add that on top of 80. So
int percent = 80 + num.nextInt(20);
The general formula is
int result = lowerBound + random.nextInt(upperBound - lowerBound);
Notes
Favor using ThreadLocalRandom over new Random(), it is faster because it sacrifices thread-safety (which you most likely do not need). So:
Random num = ThreadLocalRandom.current();
It actually also has an additional nextInt(int, int) method for exactly this use case. See the documentation:
Returns a pseudorandom int value between the specified origin (inclusive) and the specified bound (exclusive).
The code would then just be:
int percent = ThreadLocalRandom.current().nextInt(80, 100);
All my examples so far assumed that 100 is exclusive. If you want it to be inclusive, just add 1 to it. So for example
int percent = ThreadLocalRandom.current().nextInt(80, 101);
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 3 years ago.
I am trying to generate a random number between a given range and 0.
The code given below helped me to generate a number between the given range.
(int)(Math.random() * 13 + 4);
Is it possible to modify this code to generate a value between 4 and 10 and also 0
use this for generate a value between 4 and 10
public static double getRandomDoubleBetweenRange(int 4, int 10){
double x = (Math.random()*((10-4)+1))+4;
return x;
}
I suspect that this is a homework question so I won't spoonfeed you with the correct answer but give you the tools you need to answer it yourself:
public static double random()
Returns a double value with a positive sign, greater than or equal to
0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.
Source: https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#random--
Casting a double to an int performs a narrowing primitive conversion. In the range you use and for positive numbers, you can just treat it like a floor (removing the numbers after the decimal point).
If you want to know about the details, see: https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3
Something like this would do the cause.
//stream 3 random numbers from 0 to 10 and pick any of them
int random = new Random().ints(3, 0, 11).findAny().getAsInt();
//print it
System.out.println(random);
UPDATE 2:
// make a list of 0 and 4-10
List<Integer> list = Arrays.asList(0,4,5,6,7,8,9,10);
// used for picking random number from within a list
Random random = new SecureRandom();
// get random index element from a list
int randomNumber = list.get(random.nextInt(list.size()));
// print
System.out.println(randomNumber);
How come? I thought that "+1" is the lowest number it can generate... This is the question:
"(int) Math.random()*(65535 + 1) returns a random number between:
Between 0 and 65535. <- answer
This is a question from a sololearn challenge.
The documentation of method Math.random() says:
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
It's obvious - mathematically expressed, the generated interval is <0, 1). It means, the generated number will never reach 1.0 but maximally a number a bit below (ex. 0.99). Since you multiply it with 65535, it will never reach 65535. That's why you have to add +1.
I recommend you to use the class Random and it's method nextInt(int bound) which does:
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)
Therefore:
Random random = new Random();
int integer = random.nextInt(65536); // 65535 + 1 because the number is exclusive
The way you have the code right now:
(int) Math.random()*(65535 + 1)
You will always get 0.
The Math.random() method generates a number in the range [0, 1).
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
When you multiply that number by n, it has a range of [0, n). Casting it to int truncates any decimal portion of the number, making that number 0, and anything multiplied with 0 is 0. The cast occurs first because it's a higher precedence than multiplication.
Let's add parentheses so the cast occurs after the multiplication.
(int) (Math.random()*(65535 + 1))
When you multiply the truncated number by n, it has a range of [0, n). Casting it to int after the multiplication truncates any decimal portion of the number, making the range of integers 0 through (n - 1).
If you add 1 after multiplying and casting, then the lowest number it could generate would be 1. The range before adding would be 0 through 65534, after adding it would be 1 through 65535.
(int) (Math.random()*65535) + 1
How come? I thought that "+1" is the lowest number it can generate...
That is because the +1 was placed within the brackets. See below:
(int) Math.random()*(65535 + 1) //is equivalent to
(int) Math.random()*(65536) //which is equivalent to
(int) 0.0 to number < 1.0 *(65536) //which gives you a range of..
(int) (0 * 65536) to (0.999.. * 65536) //which gives you..
(int) 0 to 65535.34464.. //converted to int gives you
0 to 65535
If you want the minimum random number to be at least 1. Add it after the random operation is done:
(int) (Math.random()*65535) + 1
Had a look around the questions on this site and could not quite find the answer I was looking for about type casting the Math.random() method from double to int.
My question is, why does Math.random only return a 0 without parentheses whereas it returns random numbers when it is contained within the parentheses?
The first part of code returns 0:
int number;
number = (int) Math.random() * 10;
System.out.println("\nThe random number is " + number);
This code works however:
int number;
number = (int) (Math.random() * 10);
System.out.println("\nThe random number is " + number);
It should be noted I have seen a few different pieces of code on typecasting whereby some programmers seem to use both ways of casting.
This code:
number = (int) Math.random() * 10;
first calculates this:
(int) Math.random()
Since Math.random() returns a number from 0 up to but not including 1, if you cast it to int, it will round down to 0. Then when you multiply 10 to 0 you get 0.
Math.random() returns a number from 0 to 1. You want to cast the result of (Math.random()*10) to int, not the number you get from Math.random itself.
Numbers get rounded down. Therefore, for example, 0.3, which you can get from Math.random, gets rounded to 0. Again, you want to round the result of 0.3 times 10, which is 3. The parenthesis is important.