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
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 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
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 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 8 years ago.
Improve this question
I need to create a program which prints something for every odd multiply of 90 so for example
90 - text
180 - nothing happens
270 - text
360 - nothing happens
etc.
Thanks a lot!
A simpler solution is to print the multiples of 180.
for(int i = 180; i < max; i += 180)
System.out.println(i);
I assume this is homework, but there is no point copying someone's answer unless you can explain it. This is for you education purposes. (And your marker will be able to pick up code you probably didn't write yourself)
Your basic structure here should be pretty simple - just use a multiplier and an if statement with a modulus operator.
For example (pseudo-code - check a tutorial if you can't implement this idea):
int multiplier=1;
int maxMultiplier = 10;
int value = 0;
while (multiplier < maxMultiplier) {
value = 90 * multiplier;
if (multiplier % 2 == 0) {
// print something;
}
multiplier++;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am having trouble generating this code which I'm sure I'm just in a coder's block or something because it seems as though it should be easy but can't get it for the life of me.
I have a program which needs random numbers generated within a certain range which is to represent money. The stipulation is that the money need be in between 2 and 200 and represent only even dollars only so $2, $4, $6...so on. I have done extensive searching online which yield a bounty of code to represent random numbers in a range in java but not the part about being only even.
Any ideas?
If you wanted to be clever, you could make sure the least significant bit of the number is not set:
int num = (new Random().nextInt(199) & ~1) + 2;
This will ensure that the number is always even.
Thanks Eyal Shneider and Omaha
Marcelo's comment from the OP is the correct answer, though.
Get a number between 1-100, and multiple by 2:
int num = (new Random().nextInt(100) + 1) * 2;
int rand = new Random.nextInt(200);
int result = 2;
while(result < rand) {
result += 2;
}
return result;
(I'd make it recursive but I've got to go meet my wife for dinner.)
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I'm doing some homework and just want to make sure I am on the right track.
The question is:
Describe the order of magnitude of each of the following functions using Big-O notation.
1) N^2 + 3N = my answer O(N^2)
2) 3N^2 + N = my answer O(N^2)
3) N^5 + 100N^3 + 245 = my answer O(N^5)
4) 3Nlog2N + N^2 – my answer O(N^2)
5) 1 + N + N^2 + N^3 + N^4 = my answer O(N^4)
6) (N * (N – 1)) / 2 - my answer O(N^2)
Am I doing this right? Any suggestions?
The Big-O Notation's Order of magnitude is the one with the highest power (because they are, in most cases, the most computationally expensive function). So, you will have to see, in your formula, which function is most computationally expensive to do.
The first 2 is correct. The other 3, well.... ;)
Update: Question 1, 2, 3 and 6 are correct.