This week's giveaway is in the Android forum. We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line! See this thread for details.

Hi, I am new to this forum . I have an assignment in which i have to generate a random number using Math.random(). I know how to get a random number, let's say to 100, but I don't know how to generate a random number from -100 to 100. Hope you can help me

Michael Marhi wrote:Hi, I am new to this forum . I have an assignment in which i have to generate a random number using Math.random(). I know how to get a random number, let's say to 100, but I don't know how to generate a random number from -100 to 100. Hope you can help me

So can you show us a sample of how to do it to 100? If that can be done then extending to -100 to 100 should be simple.

And welcome to JavaRanch! Please go through this FAQ to get you started with JavaRanch guidelines

Michael Marhi
Greenhorn

Joined: Nov 21, 2010
Posts: 6

posted

0

For 0 to 100 I use:

combination = (int)(Math.random()*100)+1;

As i know, Math.random generates a random number from 0 to 1, so if I want to generate a random nubmer to 100, I multiply that with 100 and then I add 1, beacuse I don't want to have 0 as my random number.

I'll give you a hint: Check the nextInt(int n) API here. It only generate a positive random number, you'll have to think about a logic to use the random value and generate a value within the range you provide.

Michael Marhi wrote:As i know, Math.random generates a random number from 0 to 1, so if I want to generate a random nubmer to 100, I multiply that with 100 and then I add 1, beacuse I don't want to have 0 as my random number.

You know how to choose random numbers from a range between 0 and X, and then adjust the result so it's between 1 and X+1. So why can't you choose a random number from a range between 0 and X, and then adjust the result so it's between -100 and X-100?

Michael Marhi
Greenhorn

Joined: Nov 21, 2010
Posts: 6

posted

0

Thanks for your help, I managed to solve this. I hope this works combination=(int)(Math.random()*100)+1 - (int)(Math.random()*200)+1;. That should generate a random number from 1 to 100 and the right part of the code should generate a random number from 1 to 200. So if I subtract those two I get a random number between 100 and -100.

Michael Marhi wrote:Thanks for your help, I managed to solve this. I hope this works combination=(int)(Math.random()*100)+1 - (int)(Math.random()*200)+1;. That should generate a random number from 1 to 100 and the right part of the code should generate a random number from 1 to 200. So if I subtract those two I get a random number between 100 and -100.

Test it as it's not correct. You only need to generate one random number and then subtract a fixed number.

Michael Marhi wrote:Thanks for your help, I managed to solve this. I hope this works combination=(int)(Math.random()*100)+1 - (int)(Math.random()*200)+1;. That should generate a random number from 1 to 100 and the right part of the code should generate a random number from 1 to 200. So if I subtract those two I get a random number between 100 and -100.

what happens if the first random number you generate is 1, and the second random number you generate is 200?

you need to think about the RANGE of numbers - i.e. how many distinct values do you want to get?

Then, you need to think about the offset. If you always generate a number from 0-10, but you want values from -5 to 5, how can you 'shift' your output?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Michael Marhi
Greenhorn

Joined: Nov 21, 2010
Posts: 6

posted

0

OK, the last code cleary doesn't work as I want to. How about combination=(int)(Math.random()*200)+1 - 100;? I think that should do the trick

Michael Marhi wrote:OK, the last code cleary doesn't work as I want to. How about combination=(int)(Math.random()*200)+1 - 100;? I think that should do the trick

That looks approximately correct but it's easy to get off-by-one errors. So let's clarify: when you said "between -100 and 100" did that mean you wanted to get all of the 201 numbers in that range with equal probability? When you said "between 1 and 100" you wanted any of those 100 numbers with equal probability, so you had "*100" in your solution. So now when you're choosing between 201 numbers...

Michael Marhi
Greenhorn

Joined: Nov 21, 2010
Posts: 6

posted

0

Yes, I want to have all 201 numbers with equal probability. So if I understand you correctly i must replace 200 with 201? So that should look like this combination=(int)(Math.random()*201)+1 - 100;.

You should once again check for off-by-one errors. Maybe it gets clearer if you further simplify your term ( +1-100 ).

Michael Marhi
Greenhorn

Joined: Nov 21, 2010
Posts: 6

posted

0

How about this combination=(int)(Math.random()*201) - 100;? That should generate a random nubmer between 0 and 200 and if I subtract 100 from that I think it's OK.