Re: Math.random()



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
.



Relevant Pages

  • Re: find a number
    ... I have an array of 1001 integers. ... The integers are in random order, ... Here I need an algorithm to find the repeated number. ... You all think I'm paranoid, ...
    (comp.programming)
  • Re: find a number
    ... I have an array of 1001 integers. ... The integers are in random order, ... Here I need an algorithm to find the repeated number. ... public static final int[] populateArray{ ...
    (comp.lang.java.programmer)
  • Re: Preserve order in a HoH
    ... The whole point of a hash is store things in a random order that is ... structure on an array, or you need to maintain a parallel array that ... the joys of managing complex data structures... ...
    (perl.beginners)
  • Re: array random sort
    ... algorithm, RemoveAt itself is O, making the overall algorithm O. ... the input array get big, it's going to get very slow. ... >> items in it are and I want to get it to be in a random order ... >> lists sort method, but i'm not exactly sure how you do it. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Distinct Random number generator
    ... the range and generating them in a random order. ... shuffling algorithm of some kind. ... Then read out the array in order to get your ... which makes it better than using flags. ...
    (comp.lang.verilog)