This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# Random numbers

Michael Marhi
Greenhorn
Posts: 6
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

Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
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.

Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
And welcome to JavaRanch! Please go through this FAQ to get you started with JavaRanch guidelines

Michael Marhi
Greenhorn
Posts: 6
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.

Vijitha Kumara
Bartender
Posts: 3913
9
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.

And welcome to JavaRanch, Michael

Paul Clapham
Sheriff
Posts: 20955
31
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
Posts: 6
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.

pete stein
Bartender
Posts: 1561
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.

fred rosenberger
lowercase baba
Bartender
Posts: 12097
30
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?

Michael Marhi
Greenhorn
Posts: 6
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

Paul Clapham
Sheriff
Posts: 20955
31
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
Posts: 6
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;.

Hauke Ingmar Schmidt
Rancher
Posts: 436
2
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
Posts: 6
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.

 Consider Paul's rocket mass heater.