• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I convert Math.random to generate a random number between 1-1000  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very new to programming. This is for a college assignment. It says in the brief of the assignment that we will need to convert Math.random to output a random number between 1-1000. How can I do this? Thanks
 
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

This is what the Oracle Tutorial says.

Random Numbers

The random() method returns a pseudo-randomly selected number between 0.0 and 1.0. The range includes 0.0 but not 1.0. In other words: 0.0 <= Math.random() < 1.0. To get a number in a different range, you can perform arithmetic on the value returned by the random method. For example, to generate an integer between 0 and 9, you would write:

int number = (int)(Math.random() * 10);
By multiplying the value by 10, the range of possible values becomes 0.0 <= number < 10.0.

Using Math.random works well when you need to generate a single random number. If you need to generate a series of random numbers, you should create an instance of java.util.Random and invoke methods on that object to generate numbers.


Have you tried something similar yet?
 
Cameron Finch
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that helps a lot. I will try that now, I was completely clueless before. They like doing that to us in college. They give us 2 assignments to choose from. 1 that is easy and a more difficult one that involves things that we havent been thought.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch (again)

I suggest you create a Random object. Go through that link and you will find a method which does exactly what you want. But read its details very very carefully about whether you will get 1000 as a possible result or not.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something that I was confused about for years...

There is the number of distinct values you want, and there is the offset from 0 where they start. For example, if I wanted the numbers from 11-20 or 101-110, both of those have the exact same number of possibilities - ten. Then I need to map those ten possible returned values to the ten values I want.

In both cases, I could generate the values 0-9, and then add something (the offset). In the first case, i'd add 11, IN the second, I'd add 101.

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not the only person to get confused about that, Fred. I see it all the time. People think myRandom.nextInt(10) might return 10 and it never does.
 
Cameron Finch
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I got it working.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome Did you use 1 + (int)(Math.random() * 1000) or 1 + myRandom.nextInt(1000)?
 
Cameron Finch
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used 1 + (int)(Math.random() * 1000).
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cameron Finch wrote:I used 1 + (int)(Math.random() * 1000).

And, as Campbell tried to tell you, you could just as easily have used:However, what you have is fine; just remember Campbell's words if you ever need to do it again.

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for telling us. As Winston says, both versions work, but I still probably prefer the nextInt method. There is a possible pitfall with 1 + (int)(Math.random() * 1000.0)
If you get one pair of () even slightly out of place, you will get the wrong result.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like Campbell's way cause I don't like the cast to an integer that is mandatory with
Math.random().
But I wasn't aware of the other way till Campbell suggested it and Fred explained how it works.

Thank you.
Chan
 
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brilliant, Campbell and Fred. I love learning new stuff.

My only preference for Cameron's usage is the assignment (seemed to) specifically called Math.random.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

The advantage of a forum like this is that lots of people look at your posts. If you get varying or even conflicting opinions, you are suddenly in a position where there is lots to learn. You also get warnings about potential pitfalls.
 
Ranch Hand
Posts: 50
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually multiplying it by 1000 gives you a number between 1 and 1000. Not used Math.random for a while so tell me how it works out .
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher McKay wrote:Usually multiplying it by 1000 gives you a number between 1 and 1000. . . .
that is what lots of people think, but it isn't correct. It says clearly, earlier in this thread, how to get a number between 1..1000 inclusive. I still would prefer(int)(Math.random() * 1000) will give you a pseudo‑random int between 0..999. Putting the () in the wrong places around the cast will probably give you a pseudo‑random int between 0..0 
 
Christopher McKay
Ranch Hand
Posts: 50
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Christopher McKay wrote:Usually multiplying it by 1000 gives you a number between 1 and 1000. . . .
that is what lots of people think, but it isn't correct. It says clearly, earlier in this thread, how to get a number between 1..1000 inclusive. I still would prefer(int)(Math.random() * 1000) will give you a pseudo‑random int between 0..999. Putting the () in the wrong places around the cast will proabbly give you a pseudo‑random int between 0..0 

Thanks for correcting me.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have quoted this thread many times and not noticed I had made a syntax error.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have corrected a syntax error.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!