- 1

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?

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.

- 3

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

`1 + (int)(Math.random() * 1000)`or

`1 + myRandom.nextInt(1000)`?

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

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

`1 + (int)(Math.random() * 1000.0)`

If you get one pair of () even slightly out of place, you will get the wrong result.

Math.random().

But I wasn't aware of the other way till Campbell suggested it and Fred explained how it works.

Thank you.

Chan

My only preference for Cameron's usage is the assignment (seemed to) specifically called Math.random.

There are worse crimes than burning books. One of them is not reading them. Ray Bradbury

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.

- 2

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‑randomChristopher McKay wrote:Usually multiplying it by 1000 gives you a number between 1 and 1000. . . .

`int`between 0..999. Putting the () in the wrong places around the cast will probably give you a pseudo‑random

`int`between 0..0

Campbell Ritchie wrote:

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‑randomChristopher McKay wrote:Usually multiplying it by 1000 gives you a number between 1 and 1000. . . .

intbetween 0..999. Putting the () in the wrong places around the cast will proabbly give you a pseudo‑randomintbetween 0..0

Thanks for correcting me.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |