aspose file tools*
The moose likes Beginning Java and the fly likes Random numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random numbers" Watch "Random numbers" New topic
Author

Random numbers

Michael Marhi
Greenhorn

Joined: Nov 21, 2010
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

Joined: Sep 08, 2007
Posts: 3068
    
  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 | My Blog
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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

Joined: Mar 24, 2008
Posts: 3829

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


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

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

Joined: Feb 23, 2007
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

Joined: Oct 02, 2003
Posts: 11302
    
  16

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

Joined: Oct 14, 2005
Posts: 18564
    
    8

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

Joined: Nov 18, 2008
Posts: 433
    
    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

Joined: Nov 21, 2010
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Random numbers