• Post Reply Bookmark Topic Watch Topic
  • New Topic

int index = (int)(Math.random() * array.length);  RSS feed

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends

can you explain what this piece of code mean in detail

int index = (int)(Math.random() * array.length);

thank you
 
Ranch Hand
Posts: 924
9
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumibily it tries to select randomly an index of the array.Math.random returns a double
in the range of [0,1), so index is always set with a value within the indexes range of the array. I used to use Random.nextInt method for extract pseudocasual ints within a hiven range.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Claude Moore
Ranch Hand
Posts: 924
9
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder if my reply is wrong and, if it's so, why.. can you correct me, Ritchie?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Claude Moore
Ranch Hand
Posts: 924
9
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!