Re: Math.random()
- From: Lew <lew@xxxxxxxxxxxxx>
- Date: Mon, 25 Feb 2008 22:49:24 -0500
RedGrittyBrick wrote:
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.
It sounds like the OP is asking for something different: A collection of n unique numbers drawn from the range 0 to u (or equivalently, 1 to u + 1), u >= n-1. Order doesn't seem to matter.
But the OP also said:
(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..))
That means the OP can recast the *goal* to what RedGrittyBrick said. Present the n unique image names in a RandomAccess like ArrayList, or in an array. Now the OP's goal reduces to randomly ordering the n indexes into the RandomAccess, taken over the range 0 to n, not 1 to n + 1. Present the images from the RandomAccess in the random order generated by RedGrittyBrick's algorithm.
A classic case of recasting the tactic - picking random integers with no duplicates - to the goal - presenting a known list of images in random order - in order to find a better tactic - using the 0 to n (un)sorting algorithm.
--
Lew
.
- References:
- Math.random()
- From: maya
- Re: Math.random()
- From: maya
- Re: Math.random()
- From: Hal Rosser
- Re: Math.random()
- From: RedGrittyBrick
- Math.random()
- Prev by Date: Re: Math.random()
- Next by Date: Re: Math.random()
- Previous by thread: Re: Math.random()
- Next by thread: Re: Math.random()
- Index(es):
Relevant Pages
|
|