posted 3 years ago

Welcome to the Ranch

I can see exactly what that code is supposed to do, but there is a far better way to do it. Look at this thread about randomising things. You will see lots of different opinons, and also some subtle errors. If you can't get a solution from that thread, ask again.

I can see exactly what that code is supposed to do, but there is a far better way to do it. Look at this thread about randomising things. You will see lots of different opinons, and also some subtle errors. If you can't get a solution from that thread, ask again.

Campbell Ritchie

Marshal

Posts: 56600

172

posted 3 years ago

No, your answer is not wrong. I believe there is a better way to find numbers randomly in a certain range, however. You did mention Random#nextInt, which I consider far better than Math#random.

If you find the code for the Math.java class, which is in the src.zip file in your Java® installation folder, you will find that the Math#random method uses a Random object behind the scenes.

If you find the code for the Math.java class, which is in the src.zip file in your Java® installation folder, you will find that the Math#random method uses a Random object behind the scenes.

posted 3 years ago

The right-hand of the assignment statement is randomly choosing a position from the array and assigning it to the variable 'index'. The call to the Math.random() method creates a decimal of double type between 0 and 1 (exclusive) then multiplies it by the length of the array. The array length is used so as not to go out of bounds. The result has to be cast as an integer because it would be a double otherwise and that won't fit into an integer type variable. The only problem here, I think (I'm still a beginner at this) is that the first and last positions of the array (0 and array.length - 1) will never be chosen because Math.random() never becomes 0 or 1. Casting the result will truncate (lop off) the decimal part leaving you with a whole number which is assigned to index which represents the position in the array.

posted 3 years ago

That's a pretty good explanation, Keith. Just one thing though... it is possible for Math.random() to produce a number between 0 and 1, and then truncating that does result in zero. So it is possible for the first position of the array to be chosen.

(And theoretically it's possible for Math.random() to actually produce zero as its result, but you might have to wait longer than the universe is going to last to observe that.)

(And theoretically it's possible for Math.random() to actually produce zero as its result, but you might have to wait longer than the universe is going to last to observe that.)

posted 3 years ago

The same is valid for each value, as 0.2 or 0.75, if Math.random() is a continous distribution.Thus we need discretization.

If i remember well..

Paul Clapham wrote:(And theoretically it's possible for Math.random() to actually produce zero as its result, but you might have to wait longer than the universe is going to last to observe that.)

The same is valid for each value, as 0.2 or 0.75, if Math.random() is a continous distribution.Thus we need discretization.

If i remember well..

posted 3 years ago

Yes... supposedly Math.random() produces any double value in the range [0, 1) with equal probability. There are a lot of double values in that range, so yes, the probability of producing exactly 0.75 is the same as the probability of producing exactly zero, namely the reciprocal of the number of doubles in that range.

(The documentation hedges and says the results are an "approximately" uniform distribution, but that doesn't affect what I said by very much.)

(The documentation hedges and says the results are an "approximately" uniform distribution, but that doesn't affect what I said by very much.)

Campbell Ritchie

Marshal

Posts: 56600

172

posted 3 years ago

If you get a double supposedly randomly distributed between 0 and 0.999999999… and multiply it by 5, there is a 20% chance that the product will be less than 1. If you cast that number to an int you get 0.

There is also a 20% chance that the number will be greater than or equal to 4, and if you cast that to an int you get 4. So you can get any number to match an array index.

On the other hand it is very easy to get the () in the wrong place and the compiler won't notice that you are now getting a number randomly selected in the range 0…0

That is why I prefer

What happens if you have a 0‑length array and ask for myRandom.nextInt(myZeroArray.length)? No idea. Never tried it. It says in the API documentation that the argument must be positive, so 0 should not be passed.

There is also a 20% chance that the number will be greater than or equal to 4, and if you cast that to an int you get 4. So you can get any number to match an array index.

On the other hand it is very easy to get the () in the wrong place and the compiler won't notice that you are now getting a number randomly selected in the range 0…0

That is why I prefer

`myRandom.nextInt(5)`. As long as you don't think there is a possibility of that call ever returning 5.What happens if you have a 0‑length array and ask for myRandom.nextInt(myZeroArray.length)? No idea. Never tried it. It says in the API documentation that the argument must be positive, so 0 should not be passed.