Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Some teacher today teach us how work Math.random in Java language. I don't understand what he told. Can some explain? I have some code:
import java.util.Random;
public class test {
public static void main(String[] args) {
Random random = new Random();
double r = Math.random() * 4.4 + 1.2;
System.out.println(r);
}
}
Next he ask us what is interval of this random. We don't know and he write:
<1.2, 5.6> --> <0, 4.4> + 1.2 --> double r = Math.random() * 4.4 + 1.2;
How can I calculate this, what is mathematical formula? <1.2, 5.6> --> <0, 4.4> + 1.2
Math.random() gives a random number between 0 and 1. Actually it's a pseudo random but that's a different story. So in order to have a random number for example between 0 and 100 you need to multiply Math.random() by 100.
That makes you range from 0..1 to 0..100 because 0x100=0 and 1x100=100.
By adding a number to the result you set the lower boundary of the range. For example if you want a number between 100 and 200 you can do:
Math.random()*100 + 100.
So in your case he multiplies it by 4.4 giving it a range 0->4.4 and then adds 1.2 giving it a lower boundary of 1.2 which makes the actual range 1.2->5.6
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How would I calculate logarithm in Java? How would I extend it to calculate for a sequence? I am trying to find solution for following:-
N different apps have different user growth rates. At a given time t, measured in days, the number of users using an app is g^t .
After how many full days will we have 1 billion total users across the N apps?
Example 1
Input: N = 1, growthRates = [1.5]
output = 52
Example 2
Input: N = 3, growthRates = [1.1, 1.2, 1.3]
output = 79
Logarithm in Java
To calculate a common logarithm in Java we can simply use the Math.log10() method:
System.out.println(Math.log10(100));
To calculate a natural logarithm in Java we use the Math.log() method:
System.out.println(Math.log(10));
To calculate a logarithm with custom base in Java (logb(n) = loge(n) / loge), we can do it by defining a method as below:
private static double customLog(double base, double logNumber) {
return Math.log(logNumber) / Math.log(base);
}
a details about mathematic and Logarithm can be found here
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have scenario where in an text box i need to enter value like : 234-89. Numbers need to randomly generated each time i run the test, "-" should be added after 3 numbers and rest two numbers should also be random. How can i do it ?
Generate two random numbers, the first one 3 digits, the second one 2 digits. Then concatenate both variables with a hyphen in between them.
If you need help generating random numbers, see this link https://www.educative.io/edpresso/how-to-generate-random-numbers-in-java
there are many options to generate random numbers in Java
Option -1
JavaFaker's fake data classes in java
or Option 2-
import java.util.Random;
Random rand = new Random();
// Generate 3digit random integers in range 0 to 999
int rand1 = rand.nextInt(1000);
// Generate 2digit random integers in range 0 to 99
int rand2 = rand.nextInt(100);
String textfortexbox=Integer.toString(rand1)+"-"+Integer.toString(rand2);
Output 999-99
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I just have a simple question. In Java, we know that the remainder of (y%x) can't be greater than x itself. Thus, we could hypothetically set all numbers less than a certain value of x, say 100. Yet what would be the opposite of this? What if we wanted to set all numbers above a certain value, say 20, to have a range [20,100]?
I was thinking that we could subtract 20 from both sides to have the range [0,80], and then take the modulus of 80, and then add 20 to it.
Am I right in believing this?
Thanks.
The Random class includes nextInt(int) which (per the Javadoc) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. So, 0-80 is
Random rand = new Random();
int v = rand.nextInt(81);
If you want it in the ragne 20-100 that would be
int v = rand.nextInt(81) + 20;
How can the remainder be greater that the divisor ? In other words you division is not complete.
If you want to of from 20-80 you can simple do
int i = 20 + randomInteger % 80;
This will be in the range of 20 -100.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am making a small RPG using Jcreator.
I give whatever character you pick damage at the start, we will take the Swordsman as an example.
I gave him 10 Damage and for the hitting damage I made the int Hit.
My problem is that this random number is not working well for damage as it is giving me damage under the actual attack range.
Hit = 1 + (int) ((Math.random() * (Damage - 1)) +1);
You need to always know the bounds of your random number generator.
In pseudocode, to generate a number from a random space with a minimum and maximum,
Result = Minimum + (Maximum - Minimum) * (Random() - RandMin) / (RandMax - RandMin)
Try this:
hit = (int)(Math.random() * range) + min;
where range = the max value you want minus the min value you want, i.e.,
int range = (max - min) + 1;
Something like the following should work, and give you a number in the range of 5 above or below your damage:
randomNum = Damage-5 + (int)(Math.random()*Damage+1);
I tested it and it seems to have worked for me, didn't get a number below 5 or greater than 15
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have to solve the following problem:
-find flowchart or algorithm and show it in java languange
"Write a Java program that prompts the user to input a four-digit positive integers between 1001 and 9999. The program then finds the reverse of that integers. For example, if the input integer i2 3245, its reverse is 5423."
My problem is that I don't know what formula to use. I have also asked my demonstrator, but he just said that the formula uses a percentage and divide. How should I approach a solution to this problem?
Since this is a learning assignment, I will give you only hints:
To get the last digit in base N, use x % N; for base ten, that would be x % 10
To drop the last digit in base N, integer-divide by N; for base ten, that would be x /= 10
Repeating this process four times and printing the digits in reverse order will give you the desired result. Since you know that the value has exactly four digits, you do not need a loop.
This might not be what the teacher accepts/wants/expects, but for educational purposes, this would be the easiest way to do it:
String input = "1234";
int result = Integer.parseInt(new StringBuilder(input).reverse().toString());
System.out.println(result):
prints
4321