# Random numbers

Michael Marhi

Greenhorn

Posts: 6

posted 5 years ago

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.

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 | My Blog

Michael Marhi

Greenhorn

Posts: 6

posted 5 years ago

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

And welcome to JavaRanch, Michael

SCJP 5 | SCWCD 5

[How to ask questions] [Twitter]

posted 5 years ago

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 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

posted 5 years ago

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

posted 5 years ago

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 workscombination=(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.

posted 5 years ago

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 wrote:Thanks for your help, I managed to solve this. I hope this workscombination=(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

Posts: 6

posted 5 years ago

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 wrote:OK, the last code cleary doesn't work as I want to. How aboutcombination=(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

Hauke Ingmar Schmidt

Rancher

Posts: 436

2

Consider Paul's rocket mass heater. |