Re: non repeating random numbers

From: Rick Rothstein (rickNOSPAMnews_at_NOSPAMcomcast.net)
Date: 05/18/04


Date: Tue, 18 May 2004 08:55:24 -0400


> Ey! Can anyone explain to me how pickedrandom(j) = 0 allows the pc to
> continually pick a random number that is non repeating?
>
> Randomize
> For i = 1 To 10
> j = Int(Rnd * 10) + 1
>
> Do Until pickedrandom(j) = 0
> j = Int(Rnd * 10) + 1
> Loop
>
> pickedrandom(j) = i
> numbersfinal(i) = j
> Next

First off, I'd say this is an inefficient looping structure. What it is
doing is looking for elements of the pickedrandon array that have not
had a number assigned to them... that's what the pickedrandom(j)=0 is
doing... one at a time and, once found, assigns random numbers to them.
The first time you Dim (or in the case of dynamic arrays, ReDim) a
numeric array, all of its elements are set to zero. Since the numbers
being assigned are from 1 to 10 (hence, non-zero), you can test the
element to see if it equals zero... if it does, it means no random
number greater than zero (from 1 to 10) has yet been assigned to it.
This is what the test in the Do Until statement is doing.

However, as I said in the beginning, this method of assigning
non-repeating random numbers is inefficient... the loop can cycle around
quite a number of times looking for elements that are equal to zero,
especially as more and more elements become filled so that it becomes
hard to find a zero-element to fill. A more efficient method is to use
the RandomizeArray subroutine contained in the example code at this
link.

http://vbnet.mvps.org/code/helpers/randomarray.htm

The method used there is to assign number in consecutive order to an
array and then use the RandomizeArray subroutine to swap them around. It
does this by only going through the array one time with no repeats
(making it an efficient method to use).

Rick - MVP



Relevant Pages

  • An isArray test (and IE bugs)
    ... While reading the ECMA spec it occurred to me that the algorithm for the Array - concat - method offered a way in for a frame independent 'isArray' test function that could be fully sanctioned by ECMA 262. ... For a host object to pass this test it would have to have a '0' property that referred to itself, a numeric length property with the value 1 and be such that assignments to its - length - property had Array-like side effects, and also that assigning to its '0' property had array-like side effects. ... one at index zero in the new array. ...
    (comp.lang.javascript)
  • RE: Need someone very familiar with arrays
    ... I need a very efficient method to turn the above array into the following: ... Now I can do this by checking each array and rewritting it but I am ... happens to be a zero it would be missed:P ...
    (microsoft.public.vb.general.discussion)
  • Re: Arrays of zero length
    ... The minor strangeness is that when you allocate an array to zero size, ... saying something like that allocating an array makes it undefined ... In f77, zero-size was disallowed, ...
    (comp.lang.fortran)
  • Re: should every thing be zero indexed?
    ... Other families of languages count from 1. ... its number is zero: I always start counting at zero". ... > as a fundamental concept in both the definition of array indices and ... > for I in foo'range loop ...
    (comp.programming)
  • Re: Subset Sum problem (w/ limited scope)
    ... The "Subset Sum" problem (stated various ways depending on what you ... a subset of those integers that sums up to zero. ... Are there going to be duplicate values in your original array? ...
    (comp.lang.fortran)