This week's book giveaway is in the Kotlin forum.We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Devaka Cooray
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Junilu Lacar
• Paul Clapham
• Knute Snortum
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• salvin francis
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Ganesh Patekar

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

Greenhorn
Posts: 14
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
• 1
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
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: 62862
203
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
Posts: 12706
50
• 3
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: 62862
203
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
Thanks guys. I got it working.

Campbell Ritchie
Marshal
Posts: 62862
203
You're welcome Did you use 1 + (int)(Math.random() * 1000) or 1 + myRandom.nextInt(1000)?

Cameron Finch
Greenhorn
Posts: 14
I used 1 + (int)(Math.random() * 1000).

Bartender
Posts: 10748
68

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: 62862
203
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
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: 235
5
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: 62862
203
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
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: 62862
203
• 2

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

Christopher McKay
Ranch Hand
Posts: 50

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

Thanks for correcting me.

Campbell Ritchie
Marshal
Posts: 62862
203
You're welcome

Campbell Ritchie
Marshal
Posts: 62862
203