Re: Math.random()



Hal Rosser wrote:
"maya" <maya778899@xxxxxxxxx> wrote in message news:fpvdus$8de$1@xxxxxxxxxxx
maya wrote:
hi,

am trying to find out how to use random() method.. what is construction
if, say, I want to generate a random no. betw. 1 and 100..

here, http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#random()
it does not show how this method is used exactly (i.e., what
construction is exactly... these detailed method-sections should show
exact constructions/examples of how method is used..)

this method apparently takes no arguments, so how do you tell it in what
range (betw what nos.) you want the random no. generated?


int randomNo = 0;
randomNo = Math.random(a no. betw. 1 and 100...) // how do you do this????


AND: if I want to look up java tutorial on this, how do you search for
class, method, etc.. you want here?
http://java.sun.com/docs/books/tutorial/
(a few years ago I had a pg bookmarked in java.sun that was a huge index
to all aspects of Java language, so all you had to do was search for
term you wanted by doing ctrl-find on the browser (or scrolling down..)
(also, this tutorial is for Java 6, do they still have tutorial for
1.5??) thank you...
what I actually want is to generate a LIST of nos, between 1 and a variable.. I found this way:

for (int i=1; i <= iPhotosL; i++) {
out.println((int)(Math.random()*iPhotosL) + " -- random no.<br>");
}

however, I would like to tell it to NOT REPEAT a no. in the list, i.e., what I want is a list betw. 1 and a given no., but that no number appears more than once in the list.. is this possible??

(I want to have option of displaying photos in a random order instead of in order of img-names (1.jpg, 2.jpg, 3.jpg, etc..))

thank you very much..

Create a boolean array 0 to <your max value> and Each time you select a number - set the the array element that has that index to true but keep another variable for the count of how many you want - but only increment the count if the array element was false before you set it to true.
Do the loop until the count equals the number you of picks you wanted.
You're welcome



If you want integers 1-n in random order, I believe the usual procedure is to create an ordered array of n integers 1..n, then iterate through positions 1-n of the array and for each position generate a random number between the current position and n, swap the number at the current position with the number at the random position.

The above is far more efficient than repeatedly picking a random number and ignoring duplicates, because towards the end you are mostly picking duplicates. For small n I imagine this doesn't matter, other than aesthetically.
.



Relevant Pages

  • Re: Math.random()
    ... if, say, I want to generate a random no. betw. ... construction is exactly... ... if I want to look up java tutorial on this, ... number - set the the array element that has that index to true but keep another variable for the count of how many you want - but only increment the count if the array element was false before you set it to true. ...
    (comp.lang.java.help)
  • Re: Math.random()
    ... if, say, I want to generate a random no. betw. ... construction is exactly... ... if I want to look up java tutorial on this, ... number - set the the array element that has that index to true but keep another variable for the count of how many you want - but only increment the count if the array element was false before you set it to true. ...
    (comp.lang.java.help)
  • Re: Math.random()
    ... if, say, I want to generate a random no. betw. ... construction is exactly... ... if I want to look up java tutorial on this, ... count if the array element was false before you set it to true. ...
    (comp.lang.java.help)
  • Re: Math.random()
    ... if, say, I want to generate a random no. betw. ... construction is exactly... ... if I want to look up java tutorial on this, ... Try putting the photos into a List object, and then calling Collections.shufflewith that list object as the parameter. ...
    (comp.lang.java.help)