programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Tim Cooke
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Paul Clapham
• Rob Spoor
• Junilu Lacar
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Piet Souris
• Carey Brown
Bartenders:

# Random numbers

Greenhorn
Posts: 6
• • Number of slices to send:
Optional 'thank-you' note:
• • 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

Bartender Posts: 3225
34     • • Number of slices to send:
Optional 'thank-you' note:
• • 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
Bartender Posts: 3225
34     • • Number of slices to send:
Optional 'thank-you' note:
• • And welcome to JavaRanch! Please go through this FAQ to get you started with JavaRanch guidelines Michael Marhi
Greenhorn
Posts: 6
• • Number of slices to send:
Optional 'thank-you' note:
• • 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.

Bartender Posts: 4109
72   • • Number of slices to send:
Optional 'thank-you' note:
• • 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 Sheriff Posts: 27525
88   • • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • 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.

Bartender Posts: 1561
• • Number of slices to send:
Optional 'thank-you' note:
• • 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.

lowercase baba Posts: 13086
67   • • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • 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: 27525
88   • • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • 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;.

Rancher
Posts: 436
2
• • Number of slices to send:
Optional 'thank-you' note:
• • 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
• • Number of slices to send:
Optional 'thank-you' note:
• • 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. • 